面向对象——继承相关操作

重写

继承后,子类可以拥有除父类继承的内容以外的其它的内容
父类不能使用在子类中添加的内容

1、关于方法
a.在子类中可以直接添加其它的方法
b.重写:
1)完全重写
重新实现从父类继承下来的方法,重写后,子类再调用这个方法的时候,就调用子类的
2)保留父类实现的功能,在添加新的功能

对象和类调用方法的过程: 先看当前类是否存在这个方法,没有才看父类有没有这个方法,如果父类没有,就看父类的父类有没有,直到找到基类为止(object)。

class Animal:
    """动物类"""

    def __init__(self):
        self.age = 0
        self.color = 0

    def eat(self):
        print('吃东西!!!')

    def shout(self):
        print('发声!!!')

    @classmethod
    def get_number(cls):
        print(100)


class Dog(Animal):
    """
    犬类
    """

    def watchdog(self):
        print('看家')

    # 重写父类的shout
    def shout(self):
        print('汪汪汪~')

    # 重写父类eat方法
    def eat(self):
        # 保留父类eat的功能
        super().eat()
        print('吃骨头')

    @classmethod
    def get_number(cls):
        print(super().get_number())


if __name__ == '__main__':
    dog = Dog()
    dog.age = 3

添加属性

对象属性的继承:通过继承init方法来继承对象属性

给当前类添加对象属性:重写init方法。
注意:如果要保留父类的对象属性使用super()

多态:同一个事物有多种形态。子类继承父类的方法,就可以对方法进行重写,一个方法就有多种形态(多态的表现)

class Person:
    """人类"""

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

    def eat(self):
        print('人在吃饭!')


class Staff(Person):
    """职工类"""

    # init方法的参数:保证在创建对象的时候就可以给某些属性赋值
    def __init__(self, name, age=0, salary=0):
        super().__init__(name, age),
        self.salary = salary

    def eat(self):
        print('职工在吃饭')


if __name__ == '__main__':
    s1 = Staff('Lucy', 22, 1000)
    print(s1.name)
# 练习
"""
声明人类:有属性:名字、年龄、性别、身高
要求创建人的时候可以给名字、性别、年龄赋初值

再创建学生类继承自人类,拥有人类的所有属性,再添加学号、成绩、电话属性
要求创建学生对象的时候可以给名字、年龄和电话赋初值
"""


class Person2:
    def __init__(self, name, age, sex=''):
        self.name = name
        self.age = age
        self.sex = sex
        self.height = 0


class Student(Person2):
    def __init__(self, name, age, phone):
        super().__init__(name, age=age)
        self.stu_id = 0
        self.score = 0
        self.phone = phone

重载

方法重载:一个类中可以有多个名字相同的方法,但是参数不一样,就叫重载。Python中不支持方法的重载,后面的会覆盖之前的。

class Student:
    # Python不支持方法的重载
    # def run(self):
    #     print('人在跑')

    def run(self, name):
        print("%s在跑" % name)


"""
运算符重载:
>、<
大于和小于符号只需要重载其中的一个,另外一个的结果,直接就是对重载所得到结果的取反的结果

+
"""


class Studen2:
    def __init__(self, name='', age=0, height=0):
        self.name = name
        self.age = age
        self.height = height

    # 重载:>
    """
    self > other
    """

    def __gt__(self, other):
        # 比较对象1>对象2的时候是比较他们的指定属性
        return self.height > other.height

    # 重载:<
    def __lt__(self, other):
        return self.age < other.age

    # 重载+
    def __add__(self, other):
        return self.age + other.age

    # 重载-
    def __sub__(self, other):
        return self.height - other.height


if __name__ == '__main__':
    s1 = Student()
    s1.run('Lisa')

    stu1 = Studen2('Bark', 16, 160)
    stu2 = Studen2('Raise', 23, 170)

    if stu1 < stu2:
        print('===')
    原文作者:GHope
    原文地址: https://www.jianshu.com/p/59e335fbb8bd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞