07 面向对象初级
类的定义和使用
class 定义类
class Computer: #类名首字母大写(驼峰法)
pass #用pass来代替未写代码
# 类的实例化
bd = Computer() #bd为实例对象
xy = Computer()
# 类中变量称之为属性,函数称之为方法
class Lap:
style = '0707' #类的属性
color = 'black'
#self指实例化本身(谁实例化了,谁就是self)
def play(self): #类的方法
print('这是一个游戏')
def video(self):
print('这是看视频')
#类中属性调用没有括号,方法调用加括号
bd.style
bd.play()
魔法方法 init
#类中自带的方法 实例化时会自动调用
class Lap:
def __init__(self,color,style,name):
self.color = color
self.style = style
self.name = name
def play(self):
print(f'{self.name}的游戏')
def video(self):
print('这是看视频')
xd = Lap('blue','0202','晓东') #实例自动调用魔法方法init
析构函数
#类中自带的方法 销毁实例对象
class Person:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def __del__(self):
print(f'{self.name}被销毁了')
bd = Person('拜登',62,'男')
del bd
继承和重用
继承
class Person:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print('f{self.name}正在吃饭')
#子类ZheJiang继承父类Person
class ZheJiang(Person):
def eat(self):
print('浙江人喜欢吃甜食')
#Person.eat(self) #同时使用父类的同名方法
super().each() #方法重用 用super替指父类
mm = ZheJiang('木木',18,'女') #子类可使用父类方法
mm.eat() #子类方法名与父类方法名相同时,使用子类的方法
多继承
class Person:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print('f{self.name}正在吃饭')
class ZheJiang(Person): #ZheJiang继承Person
def eat(self):
print('浙江人喜欢吃甜食')
super().each() #方法重用 使用父类方法
class SiChuan(Person): #SiChuan继承Person
def eat(self):
print('四川人喜欢吃火锅')
super().each() #方法重用 使用父类方法
#多继承
class HunXue(ZheJiang,SiChuan): #HunXue继承ZheJiang,ZheJiang继承SiChuan
def eat(self):
print('喜欢吃臭豆腐')
aa = HunXue('安安',15,'男') #实例化
aa.eat() #就近原则 先HunXue,再ZheJiang,再SiChuan,再Person
#使用 mro 可查看继承顺序
HunXue.mro()
#HunXue继承ZheJiang,ZheJiang继承SiChuan,SiChuan继承Person,Person继承object,object类为所有类最大的父类
class Base:
def each(self):
print('Base')
class A(Base):
def each(self):
print('A')
class B(Base):
def each(self):
print('B')
class C(A,B): #多继承 C类继承A类 同时A类继承B类
def each(self):
print('C')
super().each() #方法重用 就近原则 先打印自己的然后打印A的
pc = C() #实例化
pc.each() #会打印出C,C的父类A,以及A的父类B
练习
- 定义一个账户类,可以创建账户、存款、取款 、查询余额、以及销户等操作
class Account:
def __init__(self,name,pwd,money = 0):
self.name = name
self.pwd = pwd
self.money = money
def deposit(self,pwd,money):
if pwd == self.pwd:
if isinstance(money,(int,float)) and money > 0:
self.money += money
return f'存款成功,存款金额为{money}元,账户余额为{self.money}元'
else:
print('请输入正确的金额')
else:
print('密码错误')
def withdraw(self,pwd,money):
if pwd == self.pwd:
if isinstance(money,(int,float)) and money > 0:
if money <= self.money:
self.money -= money
return f'取款成功,取款金额为{money}元,账户余额为{self.money}元'
else:
print('余额不足')
else:
print('请输入正确的金额')
else:
print('密码错误')
def query(self,pwd):
if pwd == self.pwd:
return f'您的账户余额为{self.money}元'
else:
print('密码错误')
def close(self,pwd):
if pwd == self.pwd:
if self.money == 0:
self.name = ''
self.pwd = ''
self.money = ''
return '销户成功'
else:
print(f'您的账户尚有余额{self.money}元,不能进行销户')
else:
print('密码错误')
- 现在三个人分别去开户,存款 和 销户,请利用上面的类实现出来
class Account:
def __init__(self,name,pwd,money = 0):
self.name = name
self.pwd = pwd
self.money = money
def deposit(self,pwd,money):
if pwd == self.pwd:
if isinstance(money,(int,float)) and money > 0:
self.money += money
return f'存款成功,存款金额为{money}元,账户余额为{self.money}元'
else:
print('请输入正确的金额')
else:
print('密码错误')
def withdraw(self,pwd,money):
if pwd == self.pwd:
if isinstance(money,(int,float)) and money > 0:
if money <= self.money:
self.money -= money
return f'取款成功,取款金额为{money}元,账户余额为{self.money}元'
else:
print('余额不足')
else:
print('请输入正确的金额')
else:
print('密码错误')
def query(self,pwd):
if pwd == self.pwd:
return f'您的账户余额为{self.money}元'
else:
print('密码错误')
def close(self,pwd):
if pwd == self.pwd:
if self.money == 0:
self.name = ''
self.pwd = ''
self.money = ''
return '销户成功'
else:
print(f'您的账户尚有余额{self.money}元,不能进行销户')
else:
print('密码错误')
aa = Account('安安',123456,1000)
aa.deposit(123456,200)
aa.query(123456)
aa.withdraw(123456,1200)
aa.close(123456)