银行账户类
class Account:
def __init__(self,filepath):
self.filepath=filepath
with open(filepath,'r') as file:
self.balance=float(file.read())
def withdraw(self,amount):
self.balance-=amount
def deposit(self,amount):
self.balance+=amount
def commit(self):
with open(self.filepath,'w') as file:
file.write(str(self.balance))
#inheritate
class Checking(Account):
def __init__(self,filepath,fee):
Account.__init__(self,filepath)
self.fee=fee
def transfer(self,amount):
self.balance=self.balance-amount-self.fee
checking=Checking("account/balance.txt",1)
checking.transfer(10)
checking.commit()
注释——doc
#inheritance
class Checking(Account):
"""this class generates checking account object """
type="checking" #class variable #可被所有函数调用
def __init__(self,filepath,fee):
Account.__init__(self,filepath)
self.fee=fee
def transfer(self,amount):
self.balance=self.balance-amount-self.fee
print(jacks_checking.__doc__) #调用注释(注意空格)
OOP——Glossary(术语)
Class 类
Object instance 对象实例
Instance variable 实例变量
Class variable 类变量
Doc strings 注释
Data member 类中的含值变量
Constructor 构造函数
Methods 类函数
Instantiation 初始化类变量
Inheritance 继承
Attributes 调用类实例中的类变量