前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 面向对象之类的继承

python 面向对象之类的继承

作者头像
py3study
发布2018-08-02 16:24:57
2230
发布2018-08-02 16:24:57
举报
文章被收录于专栏:python3python3

python中什么是继承:

新类不必从头编写

新类从现有的类继承,就自动拥有了现有类的所有功能

新类只需要编写现有类缺少的新功能

继承的好处:

复用已有代码

自动拥有了现有类的所有功能

只需要编写缺少的新功能

继承的特点:

子类和父类是is关系

python继承的特点:

总是从某个类继承

不要忘记调用super().init

代码语言:javascript
复制
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...

之类还可以自己定义方法

代码语言:javascript
复制
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...

之类可以定义和父类同名的方法

代码语言:javascript
复制
#继承父类
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...

调用父类方法

代码语言:javascript
复制
#继承父类
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...

再写一个类,来继承父类

代码语言:javascript
复制
#继承父类
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里面的方法呢?

代码语言:javascript
复制
#继承父类
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里面,自己定义参数。

代码语言:javascript
复制
#继承父类
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

代码语言:javascript
复制
#继承父类
class Man(People):
    def __init__(self,name,age,money):
        #调用父类的初始化
        #People.__init__(self,name,age)
        super(Man,self).__init__(name,age) #新式类写法

那么有什么区别呢?没有区别,效果是一样的。既然这样,为什么还要用super。是因为

super不用写父类的类名,如果有一天,父类的类名,改变了,那么这一行代码就不用更改了,只需要更改继承的父类名就可以了。

推荐使用super继承父类

代码语言:javascript
复制
#class People: 经典类
class People(object): #新式类
    def __init__(self, name,age):

新式类,必须要加object

super的写法,也是新式类里面的

经典类和新式类,主要是体现在继承上,有些不同。

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档