Object Oriented Inheritance

One: Inheritance Definition

It is a way to create a new class. In Python, a newly created class can inherit one or more parent classes. The parent class is called several classes and superclasses, and the newly created classes are called subclasses and derived classes.

class Fatherclass:pass
class Motherclass:pass
class Childclass(Fatherclass):pass #单继承
class Childclass(Fatherclass,Motherclass):pass#多继承

Note: use between multiple inheritance parent classes (base classes), separate them

 

Two: How to view (class name.__base__ method to view the first parent class) or (class name.__base__ method to view all parent classes)

In Python 3 all classes inherit object

print(Childclass.__base__)#查看第一个父类
print(Childclass.__bases__)#查看所有父类
print(Fatherclass.__bases__)#object (python3所有的类都继承object)
print(Motherclass.__bases__)

Note: Tip: If the base class is not specified, the python class will inherit the object class by default. Object is the base class of all python classes. It provides the implementation of some common methods (such as __base(s)__).

 

Three: Abstraction and inheritance (inherit in abstraction first)

Abstraction is the extraction of similar or similar parts of a class

There are two levels of abstraction: 

1. Extract the similar parts of Obama and Messi into categories; 

2. Extract the similar parts of the three classes of people, pigs, and dogs as the parent class.

The main function of abstraction is to divide categories (can isolate concerns and reduce complexity)

Note: Inheritance: It is based on the result of abstraction. To realize it through a programming language, it must first go through the process of abstraction before the abstract structure can be expressed through inheritance.

Abstraction is just an action or a skill in the process of analysis and design, and classes can be obtained through abstraction

 

 

Example 1:

Cat and Dog Fight*

# Cats: eat, drink, sleep, climb trees
# Dogs: eat, drink, sleep, watch
class Pet:
    def __init__(self,name,kind,food):
        self.name = name
        self.kind = kind
        self.food = food

def eat(self):    
        print('%s吃%s'%(self.name,self.food))

    def drink(self):
        print('%s is drinking water'%self.name)

    def sleep(self):
        print('%s is sleeping' % self.name)

class Cat(Pet):
     def climb(self): # Derived method
        print('%s is climbing a tree' % self.name)

class Dog(Pet):
     def watch(self): # Derived method
        print('%s is watching the house' % self.name)

tom = Cat('Tom','Siamese cat','cat food') # The subclass uses the name (methods and static variables), if not in the subclass, use the parent's \
# Cat('Tom','Siamese cat','cat food') instantiation
# instantiate this class
    # create an empty object
    # Execute the __init__ method: the subclass does not use the parent class
hei = Dog('Xiaohei','2 Ha','dog food')
tom.eat()
hei.eat()
tom.climb()
hei.watch()

print result:

Tom eats cat food ,
Xiaohei eats dog food
, Tom is climbing a tree
, Xiaohei is watching the house

Example 2 Man and dog fight

class Animal: # Animal
     def __init__(self, name , aggr , hp ): #Method dynamic properties built-in double down method
        self.name = name     # object attribute instance attribute
        self.aggr = aggr
        self.hp = hp

class Person(Animal):    # 类名 Person
    def __init__(self,name,sex,aggr,hp):
        self.sex = sex    # derived properties
        Animal.__init__(self,name,aggr,hp)
        super().__init__(name,aggr,hp)

    def attack(self, dog ): # custom method
        print('%s打了%s'%(self.name,dog.name))
        dog.hp -= self.aggr

class Dog(Animal):
    def __init__(self,name,kind,aggr,hp):
        self.kind = kind    # derived property
        Animal.__init__(self,name,aggr,hp)
    def bite(self,person):
        print('%s咬了%s'%(self.name,person.name))
        person.hp -= self.aggr

alex = Person('alex','unknown',1,250)
# First create a Person object,
# Initialization: find the init method, and call your own

# When the parent class and the subclass have methods with the same name, the object of the subclass will only call the subclass's method
# If you want to call the method of the parent class, you need the parent class name. Method name (self, other parameters)
hei = Dog('Little Black','teddy',260,10000)
alex.attack(hei)
hei.bite (alex)

Note: When the parent class and the subclass have the same method, the subclass object can only call the subclass's method

    If you want to call the method of the parent class, you need the parent class name. Method name (self, other parameters), such as Animal.__init__ (self, name, age, aggr, hp) in the example

         You can also use super().__init__(name, age, aggr, hp)

Note: add parentheses to super, you do not need to pass the self parameter behind, other parameters remain unchanged

 

Example 3 

class Foo:
    def __init__(self):
        self.func()

    def func(self):
        print('in Foo')

class Son(Foo):
    def func(self):
        print('in son')

f = Foo()
Son()

f = Foo() is to execute all the procedures of the foo class, so print 'in Foo'

Son() is the process of executing the son class. If not, look for the parent class, if there is, look for the subclass. In this question, if the class Son(fool):pass will be found in the parent class

print result: in Foo in son

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325863540&siteId=291194637