面向对象编程中的继承与多态
1. 继承的应用:银行账户类设计
在开发银行和信用社的金融软件时,需要设计代表储蓄账户和定期存款(CD)账户的类。经过分析发现,CD 账户是储蓄账户的一种特殊形式,因此可以使用继承来实现这两个类。
1.1 储蓄账户类(SavingsAccount)
储蓄账户类需要存储账户号码、利率和账户余额。以下是 SavingsAccount 类的代码:
# The SavingsAccount class represents a
# savings account.
class SavingsAccount:
# The __init__ method accepts arguments for the
# account number, interest rate, and balance.
def __init__(self, account_num, int_rate, bal):
self.__account_num = account_num
self.__interest_rate = int_rate
self.__balance = bal
# The following methods are mutators for the
# data attributes.
def set_account_num(self, account_num):
self.__account_num = account_num
超级会员免费看
订阅专栏 解锁全文
370

被折叠的 条评论
为什么被折叠?



