天天看点

3.单继承,多继承 ,多态 , 类属性 ,类方法

基础分为单继承和多继承:

继承主要的目的是为了增加代码的复用性

单继承:

继承:子类拥有父类的所有属性和方法

3.单继承,多继承 ,多态 , 类属性 ,类方法
class Animal:
    def run(self):
        print("跑")
    def drink(self):
        print("喝")
    def eat(self):
        print("吃")
    def sleep(self):
        print("睡")


class Dog(Animal):

    def jiao(self):
        print("叫")


class GodDog(Dog):

    def fly(self):
        print("飞")


xiaotianquan = GodDog()
xiaotianquan.eat()
xiaotianquan.run()
xiaotianquan.sleep()
xiaotianquan.drink()
xiaotianquan.fly()
xiaotianquan.jiao()

# 结果:
吃
跑
睡
喝
飞
叫
           

在python中就算是私有的属性继承下来之后也是可以在子类中或者类外所创建的对象中使用

3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法

在C++中冲重写=覆盖,重定义=同名隐藏

在python中重写两种,覆盖 / 扩展

3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法

对于扩展的例子:

class Animal:
    def __run(self):
        print("跑")
    def __drink(self):
        print("喝")
    def __eat(self):
        print("吃")
    def __sleep(self):
        print("睡")
class Dog(Animal):

    def jiao(self):
        print("叫")
class GodDog(Dog):
    def jiao(self):
        print("此处是哮天犬的叫声")
        super().jiao()  # 父类中的方法、
        print("子类中的补充")
    def fly(self):
        print("飞")
xiaotianquan = GodDog()
xiaotianquan.jiao()
           
3.单继承,多继承 ,多态 , 类属性 ,类方法

在子类中使用对父类的扩展一般,不仅需要调用父类中的 方法,而且还是要在子类中添件一些特有的方法,对父类中的属性在子类中进行丰富

3.单继承,多继承 ,多态 , 类属性 ,类方法

验证:

3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法

多继承:

3.单继承,多继承 ,多态 , 类属性 ,类方法
class A:
     def test(self):
         pass
class B(A):
    def func(self):
        pass
class C(A):
    def functest(self):
        pass
class D(B,C):
    pass
d =D()
d.test()   # 这也就是C++中的菱形继承,钻石继承,C++中为了解决二义性引入的是虚继承,但是在python中没有让用户解决,而是解释器自己解决了菱形继承的二义性

           
3.单继承,多继承 ,多态 , 类属性 ,类方法

在python3中object是所有类的基类,也就是说只要你定义了一个类,那么它的基类就都是object这个类

在python2中并不是的,在2中属于经典类,经典类不会继承自object这个类,这个类中包含了很多内置的方法,

所以为了增加代码的可移植性,以后无论在新版本还是老版本中,在定义一个类的时候,都能够手动使类继承自(object)类,这样无论在新老版本都可以快乐的运行:

多态:

3.单继承,多继承 ,多态 , 类属性 ,类方法
class Dog:
    def __init__(self,name):
        self.name = name
    def game(self):
        print("蹦蹦跳的玩耍")
class GodDog(Dog):
    def __init__(self,name):
        self.name = name
    def game(self):
        print("飞上天去玩耍")
class Person:
    def __init__(self,name):
        self.name = name
    def game(self,dog):
        print("%s和%s玩耍" % (self.name,dog.name))
        dog.game()

dog = Dog("旺财")
xiaoming = Person("小明")
xiaoming.game(dog)
dog1 = GodDog("飞天旺财")
xiaoming = Person("小明")
xiaoming.game(dog1)

小明和旺财玩耍
蹦蹦跳的玩耍
小明和飞天旺财玩耍
飞上天去玩耍
           

python比C++发生多态更简单

实例:

3.单继承,多继承 ,多态 , 类属性 ,类方法

实例分为两步1.开辟空间2.调用初始化函数

一个类中方法之在内存中保存一份,变量每个实例化出来的对象都有

3.单继承,多继承 ,多态 , 类属性 ,类方法

在python中类对象也是一个特殊的对象

3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法

证明:

3.单继承,多继承 ,多态 , 类属性 ,类方法

获取类属性的两种方式,但是非常不推荐第二个方法

class Tool(object):
    count = 0
    def __init__(self):
        Tool.count += 1   # 有这句话可以看出(Tool.count)是类的属性,因为是通过类名去调用的,而不是对象的self

a = Tool()
b = Tool()
print(Tool.count,end="")

打印结果:
2
           
3.单继承,多继承 ,多态 , 类属性 ,类方法

类方法:

3.单继承,多继承 ,多态 , 类属性 ,类方法
class Tool:
    count = 0
    @classmethod
    def show_tool_count(cls):
        print("现在拥有的工具数量是%d" % Tool.count)

    def __init__(self,name):
        self.name = name
        print("创建对象成功,它的名字是%s" % self.name)
        Tool.count += 1

    # def __del__(self):
    #     print("%s" % self.name)

tool1 = Tool("斧头")
tool2 = Tool("砍刀")
tool3 = Tool("棍子")
Tool.show_tool_count()


结果:
创建对象成功,它的名字是斧头
创建对象成功,它的名字是砍刀
创建对象成功,它的名字是棍子
现在拥有的工具数量是3
           
3.单继承,多继承 ,多态 , 类属性 ,类方法
3.单继承,多继承 ,多态 , 类属性 ,类方法
class Game:
    top_score = 0
    @staticmethod
    def show_help():
        print("不要让僵尸进入你的房间")

    @classmethod
    def show_top_score(cls):
        print("历史最高分时%s" % Game.top_score)

    def __init__(self,name):
        self.name = name

    def start_game(self):
        print("%s,请开始你的游戏" % self.name)

Game.show_help()
Game.show_top_score()
game = Game("🔅")
game.start_game()

#
不要让僵尸进入你的房间
历史最高分时0
🔅,请开始你的游戏
           

继续阅读