python练习题-day22

2019-10-23 15:03:30 浏览数 (1)

1.编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生

代码语言:javascript复制
class Student:
    count=0
    def __init__(self,name,age,gender):
        self.name=name
        self.age=age
        self.gender=gender
        Student.count =1
ming=Student("小明",25,"male")
hong=Student("小红",25,"female")
lan=Student("小兰",25,"female")
print(Student.count)

2、人狗大战

代码语言:javascript复制
class Person:
    def __init__(self,name,aggr,hp,level):
        self.name=name
        self.aggr=aggr
        self.hp=hp
        self.level=level
    def atta(self,dog):
        dog.hp-=self.aggr
        print("%s踢了一脚%s,%s掉了%s点血" % (self.name, dog.name, dog.name, self.aggr))
class Dog:
    def __init__(self,name,aggr,hp,kind):
        self.name=name
        self.aggr=aggr
        self.hp=hp
        self.kind=kind
    def bite(self,person):
        person.hp-=self.aggr
        print("%s咬了一口%s,%s掉了%s点血"%(self.name,person.name,person.name,self.aggr))
guo=Dog("果果",25,100,"金毛")
ming=Person("小明",25,100,15)
guo.bite(ming)
ming.atta(guo)
print(ming.__dict__)
print(guo.__dict__)

3、正方形周长和面积

代码语言:javascript复制
class Square:
    def __init__(self,r):
        self.side=r
    def peri(self):
        return "周长是%s"%(4*self.side)
    def area(self):
        return "面积是%s"%(self.side**self.side)
s=Square(2)
print(s.peri())
print(s.area())

0 人点赞