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

Python the last object-oriented

編輯:Python

And the last one , Supplementary knowledge , Finally !!! come on. !!!

List of articles

  • Object oriented three answer feature
    • encapsulation
    • Inherit
    • polymorphic
  • polymorphic
    • Understanding polymorphism
    • Experience polymorphism
  • Class properties and instance classes
    • Class properties
      • Setting and accessing class properties
      • Modify class properties
  • Class methods and static methods
    • Class method
      • The characteristics of class methods
      • Class method usage scenario
    • Static methods
      • Static method characteristics
      • Static methods use scenarios
  • Conclusion

Object oriented three answer feature

encapsulation

  • The operation of writing properties and methods into a class is encapsulation
  • Encapsulation can add private permissions to properties and methods

Inherit

  • The subclass inherits all the properties and methods of the parent class by default
  • Subclasses can override parent class properties and methods

polymorphic

  • Pass in different objects , It produces different results

polymorphic

Understanding polymorphism

Polymorphism means that a kind of thing has many forms ,( An abstract class has many subclasses , So the concept of polymorphism depends on inheritance ).

  • Definition : Polymorphism is a way of using objects , Subclass override parent method , Call the same parent method of different subclass objects , Different execution results can be produced .
  • benefits : Flexible call , With polymorphism , Easier to write generic code , Make general programming , To adapt to the changing needs !
  • Implementation steps :
    Define parent class , And provide methods
    Defining subclasses , And override method classes
    Pass the subclass object to the caller , You can see that different subclasses have different effects

Experience polymorphism

# Define parent class 
class Dan():
def do(self):
print(' egg ')
# Inherited parent class , Override methods with the same parent class name 
class TxDan(Dan):
def do(self):
print(' Boiled in boiling water 5 minute ')
class JianDan(Dan):
def do(self):
print(' Fry the eggs in oil until golden ')
# Pass in different objects , Execute different code , That's different do function 
class Person():
def do_dan(self,dan):
dan.do()
txd = TxDan()
jd = JianDan()
# Pass in different objects 
xl = Person()
xl.do_dan(txd) # Boiled in boiling water 5 minute 
xl.do_dan(jd) # Fry the eggs in oil until golden 

Class properties and instance classes

Class properties

Setting and accessing class properties

  • Class properties are Class object The attributes that you have , It is shared by private instance objects of this class .
  • Class properties can be used Class object or Instance object visit .
# 1. Defining classes : Defining class properties 
class Food():
weight = '10 Jin '
# Create objects 
pig = Food()
chicken = Food()
# Access class implementation : Classes and objects 
print(Food.weight) # 10 Jin 
print(pig.weight) # 10 Jin 
print(chicken.weight) # 10 Jin 

Modify class properties

Classes can only be modified by class objects , Cannot be modified by an instance object , If you modify attributes through an instance object , It means to create an instance property .

class Food():
weight = '10 Jin '
pig = Food()
chicken = Food()
# Modify class properties 
# class . Class properties = value 
Food.weight = '12 Jin '
print(Food.weight) # 12 Jin 
print(pig.weight) # 12 Jin 
print(chicken.weight) # 12 Jin 
# Properties cannot be modified through objects , If you do this , An instance attribute is created 
# object . Class properties = value 
pig.weight = '20 Jin '
print(Food.weight) # 12 Jin 
print(pig.weight) # 20 Jin 
print(chicken.weight) # 12 Jin 

Class methods and static methods

Class method

The characteristics of class methods

  • Decorator required @classthod To identify it as a class method , For class methods , The first parameter must be a class object , On one side cls As the first parameter .

Class method usage scenario

  • When method Class objects are required ( For example, the norm root consists of class attributes, etc ) when , Define class methods
  • Class methods are generally used in conjunction with class properties
class Food():
__weight = 10
@classmethod
def add_weight(cls,num):
cls.__weight += num
return cls.__weight
pig = Food()
result = Food.add_weight(5)
print(result) # 15

Static methods

Static method characteristics

  • Need to go through the decorator @staticmethod To embellish , Static methods do not need to pass either class objects or instance objects ( No formal parameters self/cls)
  • Static methods Can also pass through Instance object and Class object To visit

Static methods use scenarios

  • When method You do not need to use instance objects ( Such as instance object , Instance attributes ), There is no need to use class objects ( Such as class attribute 、 Class method 、 Create instances, etc ) when , Define static methods
  • Cancel unnecessary parameter passing , advantageous to Reduce unnecessary memory consumption and performance consumption
class Food(object):
@staticmethod
def info_food():
print(' Here is meat ')
pig = Food()
pig.info_food() # Here is meat 
Food.info_food() # Here is meat 

Conclusion

That's all !!! Congratulations on your graduation


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