程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Three characteristics of Python basic object-oriented (encapsulation, inheritance, polymorphism) second, inheritance and polymorphism

編輯:Python

Three characteristics of object-oriented
    One : encapsulation : Improve the security of the program
        1. Put the data ( attribute ) And behavior ( Method ) Wrapping in class objects . Operate on attributes inside methods , Call methods outside the class object .
        such , There is no need to care about the specific implementation details inside the method , This isolates the complexity .
        2. stay Python There is no special modifier for private properties in , If the property does not want to be accessed outside the class object , Use two... At the front "_", Such as __age
    Two : Inherit : Improve code reusability
    3、 ... and : polymorphic : Improve the scalability of the program

 Inherit
Grammar format :
class Subclass class name ( Parent class 1, Parent class 2) # When defining a class, do not write parentheses to inherit. Default inheritance object,python Support for multiple inheritance , The parent class can write multiple
pass
1. If a class does not inherit any classes , Default inheritance object
2.python Support for multiple inheritance
When you define subclasses , The constructor of the parent class must be called in its constructor 

Inherit

# Inherit
class Person(object): #Person Inherit object class , Or not , If you don't write, it's class Person: This defaults to inheritance object class
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): #Student Class inherits the above Person class
def __init__(self,name,age,stu_no):
#super() Function is used to call the parent class ( Superclass ) One way , Superclass is the parent class
super().__init__(name,age) # Use super() To call the parent class's __init__ Method , And then put name and age Pass in
self.stu_no=stu_no
class Teacher(Person): #Teacher Class inherits the above Person class
def __init__(self,name,age,teachofyear):
super().__init__(name,age) # Use super() To call the parent class's __init__ Method , And then put name and age Pass in
self.teachofyear=teachofyear
stu=Student(" Zhang San ",20,"1001") # establish stu The object of the student class , And send data to the initialization attribute
tecaher=Teacher(" Li Si ",34,10) # establish tecaher The object of the teacher class
stu.info() # Call the... Inherited from the parent class info Method , Because the methods of the parent class can no longer meet the needs of the child class , So only output , Zhang San 20
tecaher.info() # Call the... Inherited from the parent class info Method , Li Si 34

 Method rewriting :
If a subclass wants to output something unique to itself , The parent class cannot provide this requirement , You can rewrite the method yourself
1. If the subclass is not satisfied with a property or method inherited from the parent class , You can subclass it ( Method body is rewritten )
2. Subclasses can be overridden by super().xxx() Call the overridden method in the parent class 
# Copy the code above , Do method rewriting
class Person(object): #Person Inherit object class , Or not , If you don't write, it's class Person: This defaults to inheritance object class
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): #Student Class inherits the above Person class
def __init__(self,name,age,stu_no):
#super() Function is used to call the parent class ( Superclass ) One way , Superclass is the parent class
super().__init__(name,age) # Use super() To call the parent class's __init__ Method , And then put name and age Pass in
self.stu_no=stu_no
# Method rewriting
def info(self): # stay Student Rewrite the parent class method in the class
super().info() #super().xxx() Call the overridden method in the parent class
print(" Student number :{0}".format(self.stu_no))
class Teacher(Person): #Teacher Class inherits the above Person class
def __init__(self,name,age,teachofyear):
super().__init__(name,age) # Use super() To call the parent class's __init__ Method , And then put name and age Pass in
self.teachofyear=teachofyear
# Method rewriting , Let the method of the parent class meet the method unique to the child class
def info(self):
super().info()
print(" Teaching age is :{0}".format(self.teachofyear)) #{0} Represents a placeholder ,0 It's the index , Express format() The first element in
stu=Student(" Zhang San ",20,"1001") # establish stu The object of the student class , And send data to the initialization attribute
tecaher=Teacher(" Li Si ",34,10) # establish tecaher The object of the teacher class
stu.info()
tecaher.info()
 polymorphic 
In short , Polymorphism is " It has many forms ", It refers to : Several times I don't know what type of object a variable refers to , You can still call the method through this variable ,
In the running process, according to the object type applied by the variable , Dynamically determine which method in the object to call .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved