"""
Auther:少校
Time:2025/4/11 17:21
越努力,越幸运
"""
# 1. 定义一个狗类和人类
# 狗的属性: 名字、年龄、体重、品种; 方法:叫唤
# 人的属性:名字、年龄、性别、狗 方法:遛狗
class Dog:
def __init__(self, name, age=3, weight=5, breed='土狗'):
self.name = name
self.age = age
self.weight = weight
self.breed = breed
def cry_out(self):
print(f'{self.name}: 汪汪汪!!!')
class Human:
def __init__(self, name, age=18, gender='男', dog=None):
self.name = name
self.age = age
self.gender = gender
self.dog = dog
def walk_the_dog(self):
if self.dog:
print(f'{self.name}牵着{self.dog.name}散步!')
else:
print('你还没有狗!')
dog1 = Dog('财财')
dog2 = Dog('来钱', breed='泰迪')
h1 = Human('小明', dog=dog1)
h1.walk_the_dog()
h2 = Human('张三', 30)
h2.walk_the_dog()
print('------------------------------------华--丽--的--分--割--线------------------------------------')
# 2. 定义一个二维坐标点类,属性:x坐标和y坐标 方法:计算一个点到另外一个点的距离
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance(self, other):
return ((self.x - other.x)**2 + (self.y - other.y)**2) ** 0.5
p1 = Point()
p2 = Point(3, 4)
print(p1.distance(p2))
print('------------------------------------华--丽--的--分--割--线------------------------------------')
# 3. 定义一个线段类,属性:起点和终点 方法:计算线段的长度
class Line:
def __init__(self, start=Point(), end=Point()):
self.start = start
self.end = end
def length(self):
return self.start.distance(self.end)
l1 = Line(end=Point(10, 8))
print(l1.length())
print('------------------------------------华--丽--的--分--割--线------------------------------------')
# 4. 定义一个圆类,属性:圆周率、半径和圆心 方法:计算面积、判断一个圆和另外一个圆是否外切
class Circle:
pi = 3.1415926
def __init__(self, r, center=Point()):
self.r = r
self.center = center
def area(self):
return Circle.pi * self.r ** 2
def is_exterior_contact(self, other):
return self.center.distance(other.center) == self.r + other.r
print('------------------------------------华--丽--的--分--割--线------------------------------------')
# 5. 创建BankAccount类模拟银行账户,实现存款、取款、查询余额功能
# 属性:账号、密码、余额、姓名、省份证号码、电话号码
class User:
def __init__(self, name, id, tel):
self.name = name
self.id = id
self.tel = tel
class BankAccount:
def __init__(self, account_number, password, user, balance=0):
self.account_number = account_number
self.password = password
self.user = user
self.balance = balance
def save_money(self, money):
self.balance += money
def withdrawal(self, money):
if self.balance < money:
print('余额不足!')
else:
self.balance -= money
def query(self):
print(f'余额:{self.balance:,.2f}')
b1 = BankAccount('19293838382', '123123', User('小明', '1928383333', '110'), 10000)
b1.save_money(5000)
print(b1.balance)
b1.withdrawal(2000)
print(b1.balance)
b1.query()32.homework
本节2763字2025-04-12 16:12:37