(1)类 class Point: def move(self): print("move") def draw(self): print("draw")
point1=Point() point1.draw() point1.x=10 point1.y=20 print(point1.x)
point2=Point() point2.x=30 print(point2.x)
(2)构造函数 --双下划线 class Point: def init (self,x,y): self.x=x self.y=y def move(self): print("move") def draw(self): print("draw")
point=Point(10,20) print(point.x)
class Person: def init (self,name): self.name=name def talk(self): print(f"Hi,I am {self.name} ")
person=Person('lynnvi') print(person.name) person.talk()
(3)继承 class Mammal: def walk(self): print("walk")
class Dog(Mammal): def bark(self): print("bark") #尽量不要有空类
class Cat(Mammal): def be_annoy(self): print("annoying")
尽量不要有空类,每一个类下面需要空两行
dog1=Dog() dog1.walk() dog1.bark()
cat1=Cat() cat1.walk() cat1.be_annoy()
查看更多关于python 基础学习系列(八)的详细内容...