python 面向对象之类的继承

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值