首页 > 代码库 > day6面向对象--继承、多态
day6面向对象--继承、多态
继承
继承:就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的属性和方法,对上面的类进行扩展。
可以什么都不写,直接继承父类,如下:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): pass class Woman(People): pass m1 = Man("chenronghua",16) m1.eat()
运行结果如下:
chenronghua is eating......
上面代码可以看出,首先定义了一个People的类,还有一个Man(People)类直接继承People类。
下面,我们在上面的Man()类中定义一个单独的方法,如下:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) class Woman(People): pass m1 = Man("chenronghua",16) m1.piao()
运行结果如下:
chenronghua is piaoing......20s......
上面,我们给Man()增加了新的方法,可以看出,能够执行。
下面,我们来重写父类。扩展新功能,子类并且要具有父类的所有方法与属性。在子类中调用父类:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): pass m1 = Man("chenronghua",16) m1.sleep()
上面代码中,子类调用了父类中的方法,要明白本质,创建实例的本质是增加了一个新的内存变量。
不同类中方法的调用:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) # m1 = Man("chenronghua",16) # m1.sleep() w1 = Woman("chenronghua",26) w1.get_birth() w1.piao()
运行如下:
chenronghua is borning a bady......
Traceback (most recent call last):
File "/home/zhuzhu/第六天/inherit.py", line 34, in <module>
w1.piao()
AttributeError: ‘Woman‘ object has no attribute ‘piao‘
从上面可以看出,继承同一个父类的子类是方法是不能相互调用的。
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def __init__(self,name,age,money): #给男的单独增加属性 People.__init__(self,name,age) self.money = money print("%s 刚出生就有%s元" %(self.name,self.money)) #构造函数的时候就会执行代码 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) m1 = Man("chenronghua",16,100) m1.sleep()
运行结果如下:
chenronghua 刚出生就有100元
chenronghua is sleeping.....
man is sleeping.....
从上面代码可以看出,__init__(self,name,age,money)类的初始化,People.__init__(self,name,age)继承,继承父类的属性,普通的继承。
下面来看看新式类中的继承。
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def __init__(self,name,age,money): #给男的单独增加属性 super(Man,self).__init__(name,age) self.money = money print("%s 刚出生就有%s元" %(self.name,self.money)) #构造函数的时候就会执行代码 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) m1 = Man("chenronghua",16,100) m1.sleep()
新式类是用super()函数来实现继承的,super(Man,self).__init__(name,age)实现继承,新式类和旧式类的差别主要体现在多继承上面。
下面看下类的多继承问题:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %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): #给男的单独增加属性 super(Man,self).__init__(name,age) #超级父类,Man继承父类name,age self.money = money print("%s 刚出生就有%s元" %(self.name,self.money)) #构造函数的时候就会执行代码 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People,Relation): def get_birth(self): print("%s is borning a bady......" %self.name) w1 = Woman("ruhua",18) m1 = Man("chenronghua",16,100) m1.make_friends(w1) #把w1实例当做参数传给make_friends,等价于obj》w1,obj.name》w1.name
上面代码中,当子类继承多个父类的时候,即便其中一个父类没有实例化,也能调用另外一个父类的方法和属性。通过子类把两个父类关联到一起。
多态
一个接口,多个重用
day6面向对象--继承、多态
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。