Python object-oriented explanation 4 (inheriting polymorphic object-oriented related methods)

Use of inheritance

The methods of the subclass are more and more complete than the methods of the parent class, otherwise the inheritance will not have much effect

class Animals(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def sleep(self):
        print(self.name + '正在睡觉')

class Dog(Animals):
    def bark(self):
        print(self.name + '正在叫')

class Pig(Animals):
    def eat(self):
        print(self.name + '吃东西')

# Dog()调用__new__方法,在调用__init__方法
# 如果Dog里没有__new__方法,会查看父类是否重写了__new__方法
# 父类里也没有重写__new__方法,会查看父类的父类,找到了object

# 调用__init__方法,Dog类没有实现,会自动找Animals父类
dog1 = Dog('大黄', 18)
print(dog1.name)  # 父类定义的属性,子类可以直接调用
dog1.sleep()  # 父类定义的方法,子类也可以直接使用
dog1.bark()
pig1 = Pig('佩奇', 11)
pig1.sleep()
pig1.eat()

Characteristics of multiple inheritance

class A(object):
    def demo_a(self):
        print('我是A类里的demo_a方法')

    def foo(self):
        print('我是A类里的foo方法')

class B(object):
    def demo_b(self):
        print('我是B类里的demo_b方法')

    def foo(self):
        print('我是B类里的foo方法')

class C(A, B):
    pass

c = C()
c.foo()
c.demo_a()
c.demo_b()
'''
我是A类里的foo方法
我是A类里的demo_a方法
我是B类里的demo_b方法
'''

class X(object):
    def foo(self):
        print('我是X类里的foo方法')

class Y(object):
    pass

class Z(X):
    pass

class M(Y):
    def foo(self):
        print('我是M类里的foo方法')

class N(Z, M):
    pass

n = N()
n.foo()  # 深度优先
print(N.__mro__)  # __mro__属性,可以列出累继承的查找顺序
'''
我是X类里的foo方法
(<class '__main__.N'>, <class '__main__.Z'>, <class '__main__.X'>, <class '__main__.M'>, <class '__main__.Y'>, <class 'object'>)
'''

Inheritance characteristics of private attributes

Private properties and private methods are not inherited

class Animals(object):
    __type = '动物'

    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.__money = 1000

    def __text(self):
        print('我是Animals类里的私有方法__text')

class Pig(Animals):
    def __demo(self):
        print('我是Pig类里的私有方法__demo方法')

ma = Pig('小马', 18)
ma._Pig__demo()  # 自己类里定义的方法,对象名._类名__方法名
ma._Animals__text()  # 可以通过 对象名._父类名__方法名 调用父类的私有方法

# 私有属性和方法,子类不会继承
print(ma._Animals__money)
# 下面两个都会报错
# print(ma._Pig__money)
# ma._Pig__text()

Implementation of polymorphism

  • Polymorphism is based on inheritance, the method of rewriting the parent class through the subclass
  • To reach different subclass objects call the same parent method, get different results
  • Improve code flexibility
class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def sleep(self):
        print(self.name + '正在睡觉')

class Student(Person):
    def __init__(self, name, age, school):
        # 子类在父类的基础上又添加自己新的属性
        # 调用父类的两种方法
        # 1. 父类名.方法名(self,参数列表)
        # Person.__init__(self,name,age)
        # 2. 使用super方法直接调用父类的方法。推荐使用第二种方法
        super(Student, self).__init__(name, age)
        self.school = school

    def sleep(self):
        print(self.name + '在课间休息的时候睡觉')

s = Student('小花', 3, '幼稚园')
s.sleep()# 小花在课间休息的时候睡觉
print(Student.__mro__)
# (<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>)

Use of polymorphism

class Dog(object):
    def work(self):
        print('狗正在工作')

class Policedog(Dog):
    def work(self):
        print('警犬正在攻击敌人')

class Blinddog(Dog):
    def work(self):
        print('导盲犬正在领路')

class Drugdog(Dog):
    def work(self):
        print('缉毒犬正在搜毒')

class Person(object):
    def __init__(self,name):
        self.name=name
        self.dog = None
    def work_with_dog(self):
        if self.dog is not None and isinstance(self.dog,Dog):
            self.dog.work()

p = Person('张三')
ad = Policedog()
p.dog = ad
p.work_with_dog()

bd = Blinddog()
p.dog = bd
p.work_with_dog()

cd = Drugdog()
p.dog = cd
p.work_with_dog()
'''
警犬正在攻击敌人
导盲犬正在领路
缉毒犬正在搜毒
'''

Object-oriented related methods

class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age


class School(object):
    pass


class Student(Person, School):
    pass


p1 = Person('张三', 18)
p2 = Person('张三', 18)
s = Student('jack', 18)

# is 的本质是获取两个的内存 id(p1)==id(p2)
print(p1 is p2)  # 身份运算符,用来判断是否是同一个对象
print(type(p1) == Person)  # 获取的就是类对象

# s 这个对象是否由Student类创建的?
print(type(s) == Student)  # True
print(type(s) == Person)  # False

# isinstance 用来判断一个对象是否由指定的类(或父类)实例化出来的
print(isinstance(s, (Student, School)))  # True
print(isinstance(s, Person))  # True

# issubclass 用来判断一个类是否是另一个类的子类
print(issubclass(Student, Person))  # True
--------------------------------------------------------------------------------------------------------------------
La la la la la la

Guess you like

Origin blog.csdn.net/hmh4640219/article/details/112872347