Python full stack development "the 14th" object-oriented three major features--inheritance

Source: Internet
Author: User

First, the combination

Combination: A combination of a class in which an object (that is, an instance) of another class is used as a data property, called a combination of classes

That is, the property of one class is the object of another class, which is the combination

Example:

The ring is made up of two circles, and the area of the circle is the area of the outer circle minus the inner circle. The circumference of the ring is the circumference of the inner circle plus the perimeter of the outer circle, and this time we first design a circular class that calculates the area of a circle and the circumference of the circle. Then in the ' ring class ' combined with the circle of the instance as its own properties to use (the purpose is to not write the area and perimeter of the method, the direct combination of the area of the circle and method to solve.) Reduced code reuse)

#求圆环的面积和周长from Math Import Piclass Circle:    def __init__ (self,r):        self.r=r    def perimater (self):        return 2*PI*SELF.R    def area (self):        return pi*self.r*self.r# print (Circle.perimater (' R ', 2)) # Print ( Circle.area (' R ', 3)) class Circle_ring: #定义一个圆环类    def __init__ (self,outside_r,inside_r):        Outside_bijiao = max ( Outside_r,inside_r)        Intside_bijiao = min (outside_r, inside_r)        self.outsize_circle = Circle (Outside_bijiao) # Instantiate a large circle  as the value of the Self.outside_circle property        self.intsize_circle = Circle (Intside_bijiao) #实例化一个小圆环    def area ( Self):        return Self.outsize_circle.area ()-self.intsize_circle.area ()    def perimater (self):        return Self.intsize_circle.perimater () +self.outsize_circle.perimater () r1 = circle_ring (10,20)  #实例化print (R1.area ()) Print (R1.perimater ())
Combination of two ways: 1. In the __init__ method inside the combination
2. On the outside combination
 #在__init__里面组合class birthdate:def __init__ (self,year,month,day): Self.ye        Ar=year self.month = Month Self.day = Dayclass course:def __init__ (self,name,price,period): #period为周期 Self.name =name self.price = Price Self.period = Periodclass teacher:def __init__ (self,name,salary , year,month,day,price,period): #那么这个里面也要把该有的属性传进去 Self.birth = BirthDate (year,month,day) #在里面组合 (passing the properties of BirthDate into Go) self.course=course (name,price,period) self.name = name Self.salary = salary# instantiation method One: Egg = Teacher ( ' Egon ', 2000,1999,12,2, ' 6 months ', ' 15800 ') #也要实例化, the attributes inside the teacher class have to be instantiated with the print (Egg.birth.month) #当然老师也有生日, Let Egg.birth.monthprint (Egg.course.period) # Instantiate method two: Egg.birth=birthdate (1996,22,4) print (egg.birth.month) 
#在类外面实例化组合class BirthDate:    def __init__ (self,year,month,day):        self.year=year        self.month = Month        Self.day = Dayclass Course:    def __init__ (self,name,price,period): #period为周期        self.name =name        self.price = Price        Self.period = Periodclass Teacher:    def __init__ (self,name,salary,course):        self.name = name        Self.salary = Salary        Self.course = course## #在外面组合. (The combination is that the property of one class is an object of another class) egg = Teacher (' Egon ', ' egg.birth=birthdate ', ' Python ') (1996,22,4) #直接给egg一个birth的属性, print ( Egg.birth.year) egg.course =course (' Python ', ' 6 months ', 15800) print (egg.course.period)

Ii. inheritance

1. Inheritance is a way to create new classes

2. A new class can create one or more parent classes that have a parent class called a base class or a superclass

3. The new class is called a derived class or subclass

The inheritance of classes in Python is divided into: single inheritance or multiple inheritance

Class ParentClass1: #定义父类    Pass class ParentClass2: #定义父类    Pass Class SubClass1 (PARENTCLASS1): #单继承, The base class is ParentClass1, and the derived class is subclass    Pass class SubClass2 (PARENTCLASS1,PARENTCLASS2): #python支持多继承, separating multiple inherited classes    with commas Pass

4. View all inherited parent classes

Print (person.__bases__) #__base __ View only the first subclass inherited from left to right, __bases__ is to view all inherited parent classes

If you do not specify a parent class, Python inherits the object class by default and object is the parent class for all Python.

Classic class: In the Python2, class Dad: Does not inherit object, such a class is called the Classic class (it is called the Classic class, not because it is classic, but because it is older)

New class: In Python3, Python inherits the object class by default (All objects)

Class Dad is equivalent to class Dad (object) in Python2 #新式类

And there's no classic class in Python3.

5. Inheritance and abstraction (first abstract after inheritance)

Abstraction: Extracting a similar or more analogous part (that is, the characteristics of extracting a class of things, increasing in scope and less common)

From a wide range to a small range of processes

Inheritance: is based on the process of abstraction, through the programming language to achieve it, it must go through the process of abstraction, in order to express the abstract structure through the way of inheritance

is a small-to-large-scale process

6. Derivation: (relativity)

1. A subclass is generated based on the parent class, and the resulting subclass is called a derived class

2. A method that is not in the parent class, which is in the subclass, is called a derived method.

3. In the parent class, there are methods of subclasses, called method overrides (that is, rewriting the methods in the parent class).

7. Several concepts to note:

1. Subclasses can use all the properties and methods of the parent class

2. If the subclass has its own method, it executes its own, and if the subclass does not have its own method, it will look for the parent class.

3. If the subclass is not found, the parent class is not found, it will be an error

4. If a method that calls the parent class is implemented in a subclass

Within the class: Super (Subclass, Self). Method name () supper (). __init__ (parameter)

Outside the class: Super (Subclass Name, object name). Method Name ()

8. Instances of inheritance

#实现调用父类的方法class Animal: #父类 base class Def __init__ (SELF,NAME,LIFE_VALUE,AGGR): self.name= name Self.li Fe_value = Life_value Self.aggr = Aggr #攻击力 def eat (self): Self.life_value + = #谁调谁的血量就增加class person        (Animal): #子类 derived class Def __init__ (self, Money, name, Life_value, AGGR): Super (). __INIT__ (name, Life_value, AGGR) Self.money = Money #派生出来的一个属性 def attack (Self,enemy): #人的派生方法 #enemy. life_value = Enemy.life_value- Self.aggr Enemy.life_value-= Self.aggrclass Dog (Animal): #子类 derived class Def __init__ (Self,name,breed, Life_value,aggr ): # animal.__init__ (Self,name,breed, Life_value,aggr) #让子类执行父类的方法 is the parent class name. Method name (parameter), even self has to pass super (). __init__ (NA        ME,LIFE_VALUE,AGGR) #super关键字, do not preach self, in the new class of # super (dog,self). __init__ (NAME,LIFE_VALUE,AGGR) #上面super是简写 Self.breed = Breed def bite (Self,person): #狗的派生方法 person.life_value-= Self.aggr def eat (self): #父类方法的重  Write super (). Eat ()      Print (' Dog is eating ') # HA2 = Dog (' Mong Choi ', ' Husky ', 20000,100) # egg = person (' Egon ', 500,1000,50) # print (EGG.AGGR) # print (ha 2.AGGR) # egg.eat () # Print (egg.life_value) # egg.eat () # Print (egg.life_value) # # ha2.eat () # Print (ha2.life_value) # # Print (Egg.life_value) # ha2.bite (egg) # print (egg.life_value) #ha2 = Dog (' Bull terrier ', ' Mong Choi ', 20000,100) print (Ha2.life_value) Ha2.eat () #如果父类有的方法子类里面也有, then called a method rewrite, do not execute the parent class, to execute the subclass of print (Ha2.life_value) super (DOG,HA2). Eat () #生掉父类的方法, However, it is not recommended to use print (Ha2.life_value) #在继承中 If a subclass has a method that executes the subclass # if the subclass does not have a method that executes the parent class's

  

Python full stack development "the 14th" object-oriented three major features--inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.