Object-oriented three major features-inheritance

Source: Internet
Author: User

First, what is inheritance:

Inheritance is a way of creating a new class.

Second, why to use inheritance

Reduce the redundancy of your code

through the code to explain
ClassStudent:def __init__(self, name, age, Sex): Self.name=name Self.age=Age Self.sex=SexdefChoice_course (self):Print('%s is choice coursing'%self.name)classTeacher:def __init__(self, name, age, Sex, tea_id): Self.name=name Self.age=Age Self.sex=Sex self.tea_id=tea_iddefSet_score (Self, student_obj):Print('%s is set_score to%s'% (Self.name, student_obj.name))
‘‘‘
I have defined here a teacher and student class, so write no problem, but there are a lot of repetitive code, how to solve this problem, it is necessary to use the Inheritance feature analysis, there are many duplicate code in the Init method,

‘‘‘
ClassPeople:
Def__INIT__ (Self, name, age, sex):
Self.name = Name
Self.age = Age
Self.sex = Sex

ClassStudent (People):
Def__INIT__ (Self, name, age, sex):
People.__INIT__ (Self, name, age, sex)#This is a class, directly through the class name.property, called to the property of another class, which is not related to inheritance
# super (Student,self) __init__ (name,age,sex) #This is done bySuperMethod that accesses the property of the parent class of the class, which is related to inheritance

DefChoice_course (Self):
Print'%s is choice coursing '%Self.name)


ClassTeacher (People):
Def__init__ (self super (Teacherself). __init__ (Name, sex)
self.tea_id = tea_id

def set_score (self Span style= "COLOR: #8888c6" >print ( '%s is set_score to%s '% ( self.name
There's a point of knowledge: two ways to invoke a parent class property
# 1, through the class name . property, just access a property of the class directly, remembering that this inheritance is useless
# 2, through super method,super. Property can access the properties of its parent class, which is related to inheritance.
 Let me tell you, in fact, a class he can inherit multiple parent classes at the same time, then there is a problem, that is the order of the lookup attributes?
#I'll talk about this. class divided into new class and classic class
#New class: Whatever inheritsObjectClass and his subclasses, these are the new classes in thePython3, all classes will inherit by defaultObjectclass, soPython3Only new classes
#Classic class: No use of inheritanceObjectClass and its subclasses, all of which are classic classes in thePython2, there are both new and classic classes

#If this class has only one parent class, then its property lookup order is(Using object to adjust properties)
#The namespace of the object, the namespace of the object corresponding to the class, the namespace of the parent class of the class, the namespace of the parent class of the class parent class .... Always findObjectClass
#If the class has more than one parent class, then his property lookup order is(Object Tuning Properties)
#Here's an example.
# class C (object):
# a = ' C '
#
# class B (object):
# a = ' B '
#
# class D (object):
# a = ' d '
#
# class F (B, C, D):
# Pass
#
# Print (F.A)
# Print (f.__bases__)
#SoFThe search order is firstB,c,dYou can try running under the various classes under theAattribute is commented out, it can be verified
#Since this is inPython3, these classes are the new class, and the new class lookup method is breadth-first: So the lookup order isF、B、C、D、ObjectThe last is to findObject
# If these classes are classic classes, look for depth first: So the order is f, b< Span style= "COLOR: #808080", object C D

# Span style= "COLOR: #808080" > sum up: How to find new classes: Breadth First
# Classic class Lookup method: Depth First

Object-oriented three major features-inheritance

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.