类的定义与调用
构造一个银联信用卡的:
- 包含如下属性:
- 顾客姓名
- 信用卡授信额度
- 当前额度
- 单次刷卡金额上限
- 2.包含如下方法:
- 分别获得上述属性的方法
- 对授信额度进行修改的方法
- 对单次刷卡金额上限修改的方法
- 实现刷卡方法,传入一个刷卡金额,先判断是否超过单次刷卡金额上限以及当前额度是否够用,如合理,则执行刷卡,将当前额度减去刷卡金额。
- 3.创建信用卡实例:
- 执行调用相应的属性和方法
-
类的继承
-
1.通过继承银联信用卡的类,构造中国银行信用卡的类
- 实现对银联信用卡类的继承;
- 新增加属性:中国银行信用卡积分、优惠店铺列表;
- 重写刷卡方法:
- 传入消费店铺名称和消费金额,如果店铺名称在优惠店铺列表中,则刷卡金额打95折;
- 每消费10元,信用卡积分增加1分;
- 保留父类刷卡方法和其他功能。
- 新增如下方法:
- 获得用户积分的方法;
- 设置优惠店铺列表的方法。
class UnionPayCard:
def __init__(self, customer_name, credit_limit, current_limit, single_transaction_limit):
self.customer_name = customer_name
self.credit_limit = credit_limit
self.current_limit = current_limit
self.single_transaction_limit = single_transaction_limit
def get_customer_name(self):
return self.customer_name
def get_credit_limit(self):
return self.credit_limit
def get_current_limit(self):
return self.current_limit
def get_single_transaction_limit(self):
return self.single_transaction_limit
def change_credit_limit(self, new_limit):
if new_limit > self.credit_limit:
self.credit_limit = new_limit
print("授信额度已修改成功!")
else:
print("修改后的授信额度不能小于当前额度!")
def change_single_transaction_limit(self, new_limit):
if new_limit <= self.single_transaction_limit:
self.single_transaction_limit = new_limit
print("单次刷卡金额上限已修改成功!")
else:
print("修改后的单次刷卡金额上限不能大于当前的上限!")
def swipe(self, amount):
if amount <= self.single_transaction_limit and amount + self.current_limit <= self.credit_limit:
self.current_limit = self.current_limit - amount
print(f"成功刷卡{amount}元,当前额度为{self.current_limit}元。")
else:
print("刷卡金额超过单次刷卡金额上限或者当前额度不足!")
class ChinaPayCard(UnionPayCard):
def __init__(self,customer_name, credit_limit, current_limit, single_transaction_limit, Shop_list, China_PayCard_point):
super().__init__(customer_name, credit_limit, current_limit, single_transaction_limit)
self.China_PayCard_point = 0
self.Shop_list = []
def discount(self,Shop_name,amount):
if Shop_name in self.Shop_list:
amount = amount * 0.95
print("享受95折优惠!")
self.China_PayCard_point = self.China_PayCard_point + amount / 10
print(f"本次消费增加{amount / 10}分")
return amount
def change_credit_limit(self, new_limit):
super().change_credit_limit(new_limit)
def change_single_transaction_limit(self, new_limit):
super().change_single_transaction_limit(new_limit)
def swipe(self, new_limit):
super().swipe(new_limit)
def get_user_points(self):
return self.China_PayCard_point
def set_discount_shops(self, shops):
self.Shop_list = shops
card = ChinaPayCard("周谷堆", 1000000, 50000, 20000, ["a","b","c"], 0)
print(card.get_customer_name())
print(card.get_credit_limit())
print(card.get_current_limit())
print(card.get_single_transaction_limit())
card.discount("b",100)
card.change_credit_limit(9999999)
card.change_single_transaction_limit(1111)
card.swipe(8)
card.swipe(600)
card.get_user_points()
card.set_discount_shops("d")
写代码时容易出现的错误:
1.店铺名称要用字符串表示
2.尝试传入六个参数,但是只能接受五个参数。(这里card本是想引用ChinaPayCard子类的,结果一直没发现...)
3. 需要确保你在调用 super().change_credit_limit()
的时候传入了 new_limit
参数
需要改为
def change_credit_limit(self, new_limit):
super().change_credit_limit(new_limit)
复习封装、继承、多态
封装
把对象的属性、方法、事件集中到一个统一的类中,并对调用者屏蔽其中的细节,是一种信息掩蔽技术
继承
一个类共享另一个类的数据属性和方法的机制称为继承,继承类是对被继承类的扩展,⼀个类是可以继承多个类的,可以把类的继承理解为类与类之间的关系,⽬的就是为了解决代码重⽤问题
多态
同样的方法(函数)--不同的方法--具有不同的实现
不同的”子类对象“对同一方法响应不同的行动就是多态,可以理解为重写父类方法