1、写一个求正方形周长和面积的类
class perimeter: def __init__(s,long): s.long = long def Perimeter(s): print((s.long*4)) def area(s): print(s.long*s.long) Object1 = perimeter(20) Object1.Perimeter() Object1.area()
2、人狗大战游戏
class human: def __init__(self,name,sex,hp,attack): self.name = name self.sex = sex self.hp = int(hp) self.attack = int(attack) def h_skills(self,NPC): print('%s攻击了%s,掉了%s血量'%(self.name,NPC.name,self.attack-NPC.hp)) NPC.hp = NPC.hp - self.attack print('剩余%s'%(NPC.hp)) class Dogs: def __init__(self,name,hp,attack): self.name = name self.hp = int(hp) self.attack = int(attack) def D_skills(self,role): print('role%s被攻击,掉了%s血量' % (role.name, self.attack - role.hp)) role.hp = role.hp - self.attack print('剩余%s'%(role.hp)) role1 = human('新建角色1','男人','100','1000') NPC1 = Dogs('小狗一号','10000','5') while 1: role1.h_skills(NPC1) if role1.hp > 0: if NPC1.hp > 0: NPC1.D_skills(role1) else: print('你总算打赢了') break else: print('你没血了,请及时充值!') break
3、创建一个老师类,老师有生日(使用组合的方式完成)
class Teacher: def __init__(self,name): self.name = name def Content(self): return """%s,您的生日是%d-%s-%s"""%(self.name,Birth.Year,Birth.Month,Birth.Day) class Birthday: def __init__(self,Year,Month,Day): self.Year = Year self.Month = Month self.Day = Day Wu_sir = Teacher('邓老师') Birth = Birthday(1970,'01','01') print(Wu_sir.Content())
4、写一个求圆面积的类
from math import pi class Round: def __init__(self,Radius): self.Radius = Radius def Perimeter(self): return self.Radius*2*pi def Area(self): return self.Radius**2*pi