Xiaoming, male, 18 years old, went up the mountain to chop wood
# Xiao Ming, male, 10 years old, went up the mountain to chop wood # Xiao Ming, male, 10 years old, driving to the Northeast # Xiao Ming, male, 10 years old, loves big health care # Lao Zhang, male, 90 years old, went up the mountain to chop wood # Lao Zhang, male, 90 years old, driving to the Northeast # Lao Zhang, male, 90 years old, loves big health care # Lao Wang, male, 70 years old, went up the mountain to chop wood # .... class Person: def __init__(self,name,male,age): self.name=name self.male=male self.age=age def drive(self): print('%s,%s,%s are old, drive to the northeast'%(self.name,self.male,self.age)) def climb(self): print('%s,%s,%s are old, go up the mountain to chop wood'%(self.name,self.male,self.age)) def hobby(self): print('%s,%s,%s is old, loves big health care'%(self.name,self.male,self.age)) ming=Person('ming','male',10) zhang=Person('zhang','male',90) wang = Person ('wang', 'male', 70) ming.drive() zhang.drive() wang.drive() ming.hobby () zhang.hobby () wang.hobby () ming.climb() zhang.climb() wang.climb()
2: Human and Dog War
# class Person: lowercase class + uppercase class name. Parentheses can be written or not # def __init__(self,name,age,sex,hp,aggr): call the double init method, the initialization object self is the object # self.name=name object property # self.age=age # self.sex=sex # self.hp=hp # self.aggr=aggr #def attack(self,dog): define method # print('%s attacked %s'%(self.name,dog.name)) # dog.hp-=self.aggr who attacks the other side loses blood # class Dog: lowercase class + uppercase class name. Parentheses can be written or not # def __init__(self, name, age, kind, hp, aggr): Load the double init method. Initialize the object, self is the object # self.name=name attribute # self.age=age # self.kind=kind # self.hp=hp # self.aggr=aggr # def bite(self, person): define properties # print('%s attacked %s'%(self.name,person.name)) # person.hp-=self.aggr who attacks the other party loses blood # # alex=Person('alxe',21,'male',100,90) Instantiate the object first and then initialize # hei=Dog('hei',2,'teddy',900,10) Instantiate object initialization first # alex.attack(hei) # hei.bite (alex) # print(alex.hp) # print (hei.hp)
Three: Calculate the perimeter and area of squares, rectangles, circles with classes and objects
# from math import pi # round # class Circle: Define a class name class lowercase, class name uppercase, () can be written or not # def __init__(self,r): Initialize call double init method self is the object # self.r=r # def area(self): define method # return pi*(self.r**2) # def por(self): define method # return 2*pi*(self.r) # c1=Circle(5) Instantiate the object first and then initialize # print(c1.r) # print(c1.area()) object.method name() # print(c1.by()) # square # class Sqare: # def __init__(self,r): # self.r=r # def area(self): # return self.r*self.r # # def by(self): # return self.r*4 # s1=Sqare(4) # print(s1.area()) # print(s1.by()) # Rectangle # class Chang: # def __init__(self,l,w): # self.l=l # self.w=w # def area(self): # return self.l*self.w # def by(self): # return (self.l+self.w)*2 # x=Chang(5,6) # print(x.area()) # print (x.por ())