OOP >>> inheritance

inherit:

  • Inheritance is a relationship, through inheritance, a class can use the methods and properties defined in another class directly
  • Inherited class or a base class is called the parent, the parent class inherits class called subclass
  • Using inheritance can reduce code duplication

# When you create a class in python3 necessarily inherit another class, specify the parent class If not, the default object class inheritance; object is the root of all classes that are direct or indirect object inheritance

 

Instructions:

In parentheses after the class name designated to inherit the name of the parent class  class class name (parent name): such as:

 

class Teacher:
    def __init__(self,name,gender,age):
        self.name = name
        self.gender = gender
        self.age = age
    def say_hi(self):
        print("hi my name is %s age is %s gender is %s" % (self.name,self.age,self.gender))
        
class Student:
    def __init__(self,name,gender,age):
        self.name = name
        self.gender = gender
        self.age = age
    DEF say_hi (Self):
         Print ( " Hi My name IS S% S% Gender Age IS IS S% " % (the self.name, self.age, self.gender))
 # Create two objects 
T1 = Teacher ( " Jack " , " man " , 20 is ) 
t1.say_hi () 
S1 = Student ( " Maria " , " Woman " , 20 is ) 
s1.say_hi () 


#   In this case two substantially identical codes, we can use inheritance to reuse code, as follows: 


class Teacher:
     DEF  the __init__ (Self, name, Gender, Age): 
        the self.name= name
        self.gender = gender
        self.age = age
    def say_hi(self):
        print("hi my name is %s age is %s gender is %s" % (self.name,self.age,self.gender))

class Student(Teacher):  #指定Teacher类继承Student类
    pass

#创建两个对象
t1 = Teacher("Jack","man",20)
t1.say_hi()
s1 = Student("Maria","woman",20)
s1.say_hi()

 

abstract:

  The example above, for example: will Teacher the Student completely same parts extracted out into another class, and have Teacher 与 Student to inherit it, this class is called 公共父类, but this type of business and the actual demand is irrelevant, in reality it is not actually present, its role is only to store the same code to reduce duplication ; abstract this process we call

  Therefore, the above example can be trimmed:

# Extract the contents of the same teachers and students form a new class, as their common parent 
class the Person :
     DEF  __init__ (Self, name, Gender, Age): 
        self.name = name 
        self.gender = Gender 
        self.age = Age
     DEF say_hi (Self):
         Print ( " hi My name Age IS IS% S% S% S Gender IS " % (self.name, self.age, self.gender))
 class Teacher (the Person) :     # specified Teacher class inheritance Person class 
    Pass 
class Student (Person) :   #Specifies the Student class inherits Person class 
    Pass 

# Create two objects 
T1 = Teacher ( " Jack " , " man " , 20 ) 
t1.say_hi () 
s1 = Student ( " Maria " , " Woman " , 20 ) 
s1.say_hi ()

 

So the idea is correct: first abstract and then inherit

 

Property search order:

  The object itself namespace >>> namespace class >>> namespace of the parent class >>> parent of the parent class namespace >>> ...... >>> Object class

  I will always look back along the inheritance, until you find, because the object is the root of all classes , so if you could not find the final object will look like!

 

Derived and covering:

  Derived:

    When the parent class provides properties can not fully meet the needs of subclasses, subclasses can add their own property or method, or covering the parent attribute that already exists, then the parent class is called a subclass derived class

    Usually sub-class will write some new code, and the parent can not be exactly the same, both are usually derived class

  Cover: overrides

    In a subclass if the parent class of the same property name appears, according to a search order of priority subclass attributes, such behavior as cover

 

Subclass using inherited methods:

  In many cases, the sub-class code in the parent class is different only a small part, but new methods have to be defined in a subclass, this time the existing parent class method can be called in a subclass to complete most of the work, subclass only a small part of the preparation of the parent class to a different code

  Method One : Use the class name called directly, the way has nothing to do with inheritance, even if there is no inheritance, as can also call: Class name of the parent class's property or method you want to tune (self) 

  Method two : using super ( class name, Self ) method.        >>>   conventional method     such as:      the parentheses may not write #

 

class Vehicle: #定义交通工具类
     Country='China'
     def __init__(self,name,speed,load,power):
         self.name=name
         self.speed=speed
         self.load=load
         self.power=power

     def run(self):
         print('开动啦...')

class Subway(Vehicle): #地铁
    def __init__(self,name,speed,load,power,line):
        #super (Subway, self) is equivalent to the instance of itself python3 super () is equivalent to the Super (Subway, Self) 
        super () .__ init__ (name, Speed, Load, Power) 
        self.line = Line 

    DEF RUN (Self) :
         Print ( ' Metro line Welcome% s ' % self.line) 
        Super (subway, Self) .run () 

class MOBIKE (Vehicle): # Mount worship bicycle 
    Pass 

Line13 = subway ( ' China subway ' , ' 180m / S ' , ' 1000 persons / me ' , ' power ' , 13 is ) 
line13.run ()

 ****** When you inherit an existing class, and you override the parent class init method, you must call the parent class initialization method of the first line of the initialization method, passing in the required parameters parent class

 

The new class and Classic:

  The new class: any explicit or implicitly inherited from the class of object it is called the new class , the class is any python3 directly or indirectly inherits Object, so  python3 in all new class 

    Classic: not Object subclass only appear in the python2

 

    The new class is the depth of inheritance, when there is a diamond inheritance, the breadth of it inherited, that is at the same level parallel to inherit the parent class

 

combination:

  An important way to reuse software in addition to inheriting There is another way, namely: combination

  Means a combination, in one class to another class of the object as a data attribute , called a combination type

  Such as:

 

class Equip: # weaponry class 
     DEF Fire (Self):
          Print ( ' Release Fire skill ' ) 

class Riven: # hero Riven class, a hero needs to have the equipment, thus requiring a combination Equip class 
     of cAMP = ' Noxus ' 
     DEF  __init__ (Self , Nickname): 
         self.nickname = Nickname
          self.equip = Equip () equipped with a # Equip generating class assigned to the attribute instance equip 
R1 = Riven ( ' sharp Wenwen ' ) 
r1.equip.fire () # can be used grouped objects held by the method produces at

 

 

###################################

When inheritance?

  By inheritance to establish a relationship between the derived class and the base class, it is a 'yes' relationship, such as the white horse is a horse, man is an animal.

  When there between classes many of the same features, extract these common features make the base class, inheritance is better, such as the teacher is a person, who is a student

When used in combination?

  In combination with the established relationship between class and class combination, it is a 'have' relationships, such as birthday professors, and professors teach python linux courses on student s1, s2, s3 ...

 

 

    

 

Guess you like

Origin www.cnblogs.com/pupy/p/11247104.html