发布时间:2018-03-25 17:22:15编辑:admin阅读(4426)
python中什么是继承:
新类不必从头编写
新类从现有的类继承,就自动拥有了现有类的所有功能
新类只需要编写现有类缺少的新功能
继承的好处:
复用已有代码
自动拥有了现有类的所有功能
只需要编写缺少的新功能
继承的特点:
子类和父类是is关系
python继承的特点:
总是从某个类继承
不要忘记调用super().init
class People(object): def __init__(self, name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) #继承父类 class Man(People): pass m1 = Man("Alin",21) m1.eat()
执行输出:
Alin is eating...
之类还可以自己定义方法
class People(object): def __init__(self, name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) #继承父类 class Man(People): def go_to_work(self): print("%s is go_to_work..." % self.name) m1 = Man("Alin",21) m1.eat() m1.go_to_work()
执行输出:
Alin is eating...
Alin is go_to_work...
之类可以定义和父类同名的方法
#继承父类 class Man(People): def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): print("Man is sleeping...") m1 = Man("Alin",21) m1.eat() m1.sleep()
执行输出:
Alin is eating...
Man is sleeping...
调用父类方法
#继承父类 class Man(People): def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): People.sleep(self) print("Man is sleeping...") m1 = Man("Alin",21) m1.eat() m1.sleep()
执行输出:
Alin is eating...
Alin is sleeping...
Man is sleeping...
再写一个类,来继承父类
#继承父类 class Man(People): def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): People.sleep(self) print("Man is sleeping...") class WoMan(People): def get_birth(self): print('%s is born a baby...' % self.name) m1 = Man("Alin",21) w1 = WoMan("Rose",26) w1.get_birth()
执行输出:
Rose is born a baby...
那么Woman是否可以执行Man里面的方法呢?
#继承父类 class Man(People): def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): People.sleep(self) print("Man is sleeping...") class WoMan(People): def get_birth(self): print('%s is born a baby...' % self.name) m1 = Man("Alin",21) w1 = WoMan("Rose",26) w1.get_birth() w1.go_to_work()
执行报错
AttributeError: 'WoMan' object has no attribute 'go_to_work'
子类之间,是无法直接调用的。
现在有了新的需求,Man在实例化的时候,需要多传一个参数,而Woman保持不变。如果直接修改父类,会影响Woman的实例化。那怎么办呢?
需要在Man里面,自己定义参数。
#继承父类 class Man(People): def __init__(self,name,age,money): #调用父类的初始化 People.__init__(self,name,age) #单独定义一个变量 self.money = money print("%s 一出生,就有 %s money" % (self.name,self.money)) def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): People.sleep(self) print("Man is sleeping...") class WoMan(People): def get_birth(self): print('%s is born a baby...' % self.name) m1 = Man("Alin",21,10) m1.eat() w1 = WoMan("Rose",26)
执行输出:
Alin 一出生,就有 10 money
Alin is eating...
调用父类方法,还有另外一种写法,使用super
#继承父类 class Man(People): def __init__(self,name,age,money): #调用父类的初始化 #People.__init__(self,name,age) super(Man,self).__init__(name,age) #新式类写法
那么有什么区别呢?没有区别,效果是一样的。既然这样,为什么还要用super。是因为
super不用写父类的类名,如果有一天,父类的类名,改变了,那么这一行代码就不用更改了,只需要更改继承的父类名就可以了。
推荐使用super继承父类
#class People: 经典类 class People(object): #新式类 def __init__(self, name,age):
新式类,必须要加object
super的写法,也是新式类里面的
经典类和新式类,主要是体现在继承上,有些不同。
class People(object): #新式类 def __init__(self, name,age): self.name = name self.age = age def eat(self): print("%s is eating..." % self.name) def talk(self): print("%s is talking..." % self.name) def sleep(self): print("%s is sleeping..." % self.name) class Relation(object): def make_friends(self,obj): print("%s is making friends with %s" % (self.name,obj.name)) #多继承 class Man(People,Relation): def __init__(self,name,age,money): #调用父类的初始化 #People.__init__(self,name,age) super(Man,self).__init__(name,age) #单独定义一个变量 self.money = money print("%s 一出生,就有 %s money" % (self.name,self.money)) def go_to_work(self): print("%s is go_to_work..." % self.name) def sleep(self): People.sleep(self) print("Man is sleeping...") class WoMan(People,Relation): def get_birth(self): print('%s is born a baby...' % self.name) m1 = Man("Alin",21,10) w1 = WoMan("Rose",26) #执行Relation的方法 m1.make_friends(w1)
执行输出:
Alin 一出生,就有 10 money
Alin is making friends with Rose
上一篇: python 面向对象之析构函数
下一篇: python 装饰器构造优酷视频登录程序
47604
45983
36909
34467
29079
25713
24565
19714
19245
17756
5564°
6155°
5690°
5737°
6704°
5482°
5484°
5988°
5965°
7295°