python创建⼀个银⾏账户类account_五⼤案例学会类属性⼿把
⼿教你⼊门Python。。。
本⽂来⾃于千锋教育在阿⾥云开发者社区学习中⼼上线课程《Python⼊门2020最新⼤课》,主讲⼈姜伟。
案例讲解
1、设计两个类:
⼀个点类,属性包括x,y坐标。
⼀个Rectangle类(矩形),属性有左上⾓和右下⾓的坐标
⽅法:1.计算矩形的⾯积;2.判断点是否在矩形内
实例化⼀个点对象,⼀个正⽅形对象,输出矩形的⾯积、输出点是在矩形内
class Point(object):
# Point ⽅法在创建时,需要两个int类型的参数,⽤来表⽰x,y坐标
def __init__(self, x: int, y: int):
self.x = x
self.y = y
class Rectangle(object):
def __init__(self, top_left: Point, bottom_right: Point):
self.bottom_right = bottom_right
def get_area(self):
# ⾯积:长 * 宽
length = abs(self.bottom_right.x - p_left.x)
width = p_left.y - self.bottom_right.y)
return length * width
def is_inside(self, point):
# if self.bottom_right.x >= point.x >= p_left.x p_left.y >= point.y >= self.bottom_right.y:
# return True
# else:
# return False
return self.bottom_right.x >= point.x >= p_left.x p_left.y >= point.y >= self.bottom_right.y
p1 = Point(4, 20) # 定义左上⾓的点
p2 = Point(30, 8) # 定义右下⾓的点
r = Rectangle(p1, p2) # 把左上⾓和右下⾓的点传递给矩形
_area()) # 312
# p = Point(10;, 13) # True
p = Point(20;, 30) # False
print(r.is_inside(p))
2、写⼀个计算器类,可以进⾏加、减、乘、除计算
class Calculator(object):
@staticmethod
def add(a, b):
return a + b
@staticmethod
def sub(a, b):
汽车入门return a - b
@staticmethod
def mul(a, b):
return a * b
@staticmethod
def div(a, b):
return a / b
print(Calculator.add(4, 5)) # 9
print(Calculator.sub(4, 5)) # -1
print(Calculator.mul(4, 5)) # 20
print(Calculator.div(4, 5)) # 0.8
3、创建⼀个Person类,添加⼀个类字段⽤来统计Person类的对象的个数
class Person(object):
__count = 0 # 类属性
def __new__(cls, *args, **kwargs):
x = object.__new__(cls) # 申请内存,创建⼀个对象,并设置类型是 Person 类return x
def __init__(self, name, age):
Person.__count += 1
self.name = name
self.age = age
@classmethod
def get_count(cls):
return cls.__count
# 每次创建对象,都会调⽤ __new__ 和 __init__ ⽅法
# 调⽤ __new__ ⽅法,⽤来申请内存
# 如果不重写 __new__ ⽅法,它会⾃动object 的 __new__
# object 的 __new__ ⽅法,默认实现是申请了⼀段内存,创建⼀个对象
p1 = Person('张三', 18)
p2 = Person('李四', 19)
p3 = Person('jack', 20)
# unt) # 3
print(p1, p2, p3)
_count()) # 3
# p4 = object.__new__(Person) # 申请了内存,创建了⼀个对象,并设置它的类型是Person
# p4.__init__('tony', 23)
# print(p4)
4、建⽴⼀个汽车类Auto,包括轮胎个数,汽车颜⾊,车⾝重量,速度等属性,并通过不同的构造⽅法创建实例。⾄少要求汽车能够加速、减速、停车。再定义⼀个⼩汽车类CarAuto继承Auto并添加空调、导航属性,并且重新实现⽅法覆盖加速、减速的⽅法
class Auto(object):
def __init__(self, color, weight, speed=0, wheel_count=4):
self.wheel_count = wheel_count
self.weight = weight
self.speed = speed
def change_speed(self, x):
"""
修改车速
:param x: 表⽰要修改的车速值。如果是正数,表⽰加速,负数表⽰减速,0表⽰停车
"""
if x == 0:
self.speed = 0 # 如果传递的参数0,表⽰要停车
return
self.speed += x
if self.speed <= 0 and x < 0
return # return 后⾯可以什么数据都不加,表⽰函数结束
class CarAuto(Auto):
def __init__(self, color, weight, ac, navigator, speed=0, wheel_count=4):
super(CarAuto, self).__init__(color, weight, speed, wheel_count)
self.navigator = navigator
self.ac = ac
car = CarAuto('⽩⾊', 1.6, '美的', 'Android', 10, 5)
car.change_speed(30)
print(car.speed) # 30
car.change_speed(-40)
print(car.speed) # 0
5.有⼀个银⾏账户类Account,包括名字,余额等属性,⽅法有存钱、取钱、查询余额的操作。要求:
1.在存钱时,注意存款数据的格式
2.取钱时,要判断余额是否充⾜,余额不够的时候要提⽰余额不⾜
class Account(object):
def __init__(self, name, balance):
self.name = name
self.balance = balance
def save_money(self, money):
assert isinstance(money, float) or isinstance(money, int), '请注意存钱数据的格式'
self.balance += money
def take_money(self, money):
if self.balance < money:
print('余额不⾜')
else:
self.balance -= money
def check_balance(self):
return self.balance
a = Account('张三', 8000)
a.save_money(10000)
a.take_money(1000)
print('当前余额:', a.check_balance())
a.take_money(20000)
print('当前余额:', a.check_balance())
发布评论