# 猜拳游戏 import random class computer: def __init__(self): self.name=0 self.scores=0 self.myrand=0 def punch(self): #电脑出拳 self.myrand=random.randint(1,3) if self.myrand==1: print("电脑出拳:石头") elif self.myrand==2: print("电脑出拳:剪刀") else: print("电脑出拳:布") class player: def __init__(self): self.name=0 self.scores=0 def punch(self): #玩家出拳 self.myrand=int(input("请出拳:1.石头 2.剪刀 3.布")) if self.myrand==1: print("玩家出拳:石头") elif self.myrand==2: print("玩家出拳:剪刀") else: print("玩家出拳:布") class games: def __init__(self,computer,player): self.computer=computer self.player=player self.sum=0 def start(self): print("欢迎使用猜拳游戏") a=input("电脑角色:1.悟空 2.比克 3.悟饭") if a=="1": self.computer.name = "悟空" elif a=="2": self.computer.name = "比克" elif a=="3": self.computer.name = "悟饭" else: print("输入错误!默认悟空") self.computer.name = "悟空 " self.player.name=input("请输入玩家名字:") while True: self.player.punch() self.computer.punch() self.win() tag=input("是否继续:y/n") if tag.lower()=="n": break #游戏结束,输入对战结果 self.results() def win(self): self.sum += 1 if self.computer.myrand==self.player.myrand: print("平局") elif (self.computer.myrand==1 and self.player.myrand==2) or (self.computer.myrand==2 and self.player.myrand==3) or (self.computer.myrand==3 and self.player.myrand==1) : print("%s赢"%self.computer.name) self.computer.scores+=1 else: print("%s赢"%self.player.name) self.player.scores+=1 def results(self): print("%s VS %s"%(self.player.name,self.computer.name)) print("%s赢了%d分" % (self.computer.name,self.computer.scores)) print("%s赢了%d分" % (self.player.name, self.player.scores)) ping=self.sum-self.computer.scores-self.player.scores print("平局%d"%ping) print("游戏结束!") p= player() c= computer() g=games(p,c) g.start()
自动销毁方法:
__del__():XXXX
del 引用
可演示删除时自动调用__del__()
程序结束时自动调用__del__() 如:
class Student:
def set_name(self,name):
self.name=name
def get_name(self):
return self.name
def __del__(self):
print("stydent对象正在销毁...")
stu=Student()
# del stu 在这里加了del 后自动销毁方法会提前 没有它自动销毁会在程序结束后
print("*"*20)
print("程序结束")
结果
********************
程序结束
stydent对象正在销毁...
<------------------------------------------------------->
可以测试一个对象被引用了多少次:如
import sys
class Student:
def set_name(self,name):
self.name=name
def get_name(self):
return self.name
def __del__(self):
print("stydent对象正在销毁...")
stu=Student()
stu1=stu
print(sys.getrefcount(stu))
结果
3
<------------------------------------------------->
面向对象的三大特征:封装·继承·多态
继承; 子类继承父类,子类可以使用父类的属性和方法,
简化代码
当生成子类对象时,先初始化父类对象,所以如果父类有
__dek__()方法,并且有属性时,要通过子类的构造赋值
父类(object)
子类(父类)
class person(object): def __init__(self,name,sex,height): self.name=name self.sex=sex self.height=height def eat(self): print("%s在吃饭..."%self.name) print("正在吃饭...") class Student(person): pass stu=Student("张三","男",1.80) stu.eat()结果:
张三在吃饭...
正在吃饭...
<------------------------------------------------------->
一个类可以有多个子类
在子类中,调用父类的属性时,在__init__()方法中使用
父类·属性,或self.属性
而调用父类方法时:super().父类方法() 或者self.父类方法()
class person(object): def __init__(self,name,sex,height): self.name=name self.sex=sex self.height=height def eat(self): print("%s在吃饭..."%self.name) print("正在吃饭...") def hello(self): print("hello大家好...") class Student(person): pass class Teacher(person): def __init__(self,name,sex,height,score): person.name=name #这里也可以用self.name=name person.sex=sex person.height=height self.score=score def stutest(self): super().hello() print("我叫李四...") stu=Student("张三","男",1.80) stu.eat() tea=Teacher("李四","男",1.78,88) tea.eat() tea.stutest()
结果:张三在吃饭...
正在吃饭...
李四在吃饭...
正在吃饭...
hello大家好...
我叫李四...
<-------------------------------------------------------------->
# 三代继承
class car(object):
def __init__(self,brand,color):
self.brand=brand
self.color=color
def run(self):
print("汽车在公路上行驶...")
def __str__(self):
return "%s品牌%s颜色的汽车在公路上行驶..."%(self.brand,self.color)
class suv(car):
def __init__(self,brand,color,seats):
car.brand=brand
car.color=color
self.seats=seats
def run(self):
super().run()
print("suv汽车在公路上行驶...")
class bigsuv(suv):
def __init__(self,brand,color,seats,changpeng):
suv.__init__(self,brand,color,seats)
self.changpeng=changpeng
def run(self):
print("大suv汽车在公路上行驶...")
# car=car("奔驰","黑色")
# print(car)
# suv=suv("奔驰","黑色",5)
# suv.run()
bigsuv=bigsuv("奔驰","黑色",7,"敞篷")
bigsuv.run()
结果:
大suv汽车在公路上行驶...
<------------------------------------------------------->
多重继承:
类同时继承多个父类,class c(a,b),当有ab均有
相同方法,而子类有重写时,调用谁的方法,如果子类没有
方法,则调用那个父类的方法?
类名.mro(),可以看到所有父类,即搜寻顺序
class a(object):
def test(self):
print(".........a...........")
class b(object):
def test(self):
print(".........b...........")
class c(b,a):
def test1(self):
super().test()
print("..........c..........")
C=c()
C.test1()
print(c.mro())
结果:
.........b...........
..........c..........
[<class '__main__.c'>, <class '__main__.b'>, <class '__main__.a'>, <class 'object'>]