Home  >  Article  >  Backend Development  >  What are the uses of inheritance in python? Detailed explanation of the usage of python inheritance

What are the uses of inheritance in python? Detailed explanation of the usage of python inheritance

不言
不言Original
2018-09-15 18:03:033078browse

What this article brings to you is about the usage of inheritance in python? The detailed explanation of the usage of Python inheritance has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Three major characteristics of object-oriented
1. Encapsulation: Encapsulate properties and methods into an abstract class according to responsibilities
2. Inheritance: Implement code reuse, the same code does not need to be repeated Write
3. Polymorphism
Single inheritance
The concept of inheritance: the subclass has all the attributes and methods of the parent class
The syntax of inheritance
class class name (parent class):
def Subclass-specific methods
"""
"""
Cat class is a subclass of Animal class, Animal class is the parent class of Cat class, Cat inherits from Animal class
Cat class is Derived class of Animal class, Animal class is the base class of Cat class, Cat class is derived from Animal class
"""
1.

class Animal(object):
    def eat(self):
        print '吃'
    def drink(self):
        print '喝'
    def run(self):
        print '跑'
    def sleep(self):
        print '睡'
class Cat(Animal):
    # 子类拥有父类的所有属性和方法
    def call(self):
        print '喵喵'
class Dog(Animal):
    def bark(self):
        print '旺旺'
class Hellokitty(Cat):
    def speak(self):
        print '我可以说日语'
# 创建一个猫对象
fentiao = Cat()
fentiao.eat()
fentiao.drink()
fentiao.run()
fentiao.sleep()
fentiao.call()

2.
Override parent Class method
1. Override the method of the parent class
2. Extend the method of the parent class
"""

class Animal:
    def eat(self):
        print '吃'
    def drink(self):
        print '喝'
    def run(self):
        print '跑'
    def sleep(self):
        print '睡'
class Cat(Animal):
    # 子类拥有父类的所有属性和方法
    def call(self):
        print '喵喵'
class Hellokitty(Cat):
    def speak(self):
        print '我可以说日语'
    def call(self):
        # 针对子类特有的需求,编写代码
        print '欧哈有~空你起哇'
        # 调用原本在父类中封装的代码
        Cat.call(self)
        # 增加其他的子类代码
        print '#!@$@!#!#'
kt = Hellokitty()
# 如果子类中,重写了父类的方法
# 在运行中,只会调用在子类中重写的父类的方法而不会调用父类的方法
kt.call()

3.

class Bird:
    def __init__(self):
        self.hungry = True
    # 鸟吃过了以后就不饿了
    def eat(self):
        if self.hungry:
            print 'Aaaaahhh...'
            self.hungry = False
        else:
            print 'No thanks'
class SongBird(Bird):
    def __init__(self):
        self.sound = 'Squawk!'
        Bird.__init__(self)
    def sing(self):
        print self.sound
littlebird = SongBird()
littlebird.eat()
littlebird.sing()

4.

class A:
    def test(self):
        print 'A-----test 方法'
    def demo(self):
        print 'A-----demo 方法'
class B:
    def test(self):
        print 'B------test 方法'
    def demo(self):
        print 'B-------demo方法'
class C(B,A):
    """多继承可以让子类对象,同时具有多个父类的属性和方法"""
    pass
# 创建子类对象
c = C()
c.test()
c.demo()

Related recommendations:

Single inheritance and multiple inheritance in Python

Python class What is inheritance? What are the rules for class inheritance?

The above is the detailed content of What are the uses of inheritance in python? Detailed explanation of the usage of python inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn