Article reference:
Python Object Oriented (beginner)
Python Object Oriented (advanced text)
Python is an object-oriented language. This article is a primer of object-oriented programming, which only introduces encapsulation and does not involve inheritance and polymorphism. Because C + + is also an object-oriented language, there are many similarities.
members of a class
Members of a class can be grouped into three broad categories: variables , methods , and properties
variable Ordinary variables belong to objects static variables belong to the class, that is, no matter how many objects are instantiated, static variables have only one
Class student:
# static variable
type = "Human"
def __init__ (self, ID, name):
# normal variable
self.id = ID
Self.name = name
# access static variable
print (student.type)
# Access Common variable
s = Student (3, "Mick")
print (s.name) C19/>print (S.type) # Static variables can also be accessed by objects
Method Normal method : Called by an object, at least one self parameter, when the normal method is executed, the object calling the method is automatically assigned to the self class method : By a class or object invocation, at least one CLS parameter, when the class method is executed, Automatically copies the class that calls the method to the CLS static method : Called by a class or object, with no default parameters
Class Student:
type = "Human"
def __init__ (self, ID, name):
self.id = id
self.name = name
# common method
Def show (self):
print ("ID: {0}\t name: {1}". Format (self.id, self.name))
# class method
@classmethod
def Another_init (CLS, ID, name): Return
CLS (ID, name)
# static method
@staticmethod
def Hello ():
print (" Hello ")
s = student.another_init (3," Mick ")
S.show ()
S.hello ()
student. Hello ()
same point : All three methods belong to the class, there is only one on the memory.
different points : The difference between the three methods is that the normal method must be called by the object, because the first parameter of the normal method is the object itself, and the class method and the static method can be invoked either by object or by class. The difference is that the class method also passes in the class as a parameter, while the static method does not have the parameters that must be passed in.
Property
The attribute in Python is actually a variant of the normal method, called : without parentheses, like accessing a variable
How properties are defined adorners : Applying an adorner static variable on a method: defining a static variable in a class with a Property object
Python3 canceled the classic class, only the new class.
First, the adorner way: @property, @ method name. Setter, @ Method name. deleter
Class Student:
def __init__ (self, ID, name):
# normal variable
self.id = id
self.name = name
@property # Decorated by property, this class method becomes read attribute
def myname (self): return
self.name
@myname. Setter # Decorated by setters, this class method becomes write property
def myname (self, name):
self.name = name
@myname. deleter # Decorated by deleter, this class method becomes write attribute
def MyName (self):
del self.name
s = student (3, "Mick")
print (s.myname)
s.myname = "Chen"
print ( S.myname)
del s.myname
static variable mode : Constructing Property Object
Class Student:
def __init__ (self, ID, name):
# normal variable
self.id = id
self.name = name
def get_name (self) : Return
self.name
def set_name (self, name):
self.name = name
def del_name (self):
del self.name
# parameter corresponding to fetch, set, delete
MyName = property (Get_name, Set_name, del_name)
s = student (3, "Mick")
print ( S.myname)
s.myname = "Chen"
print (s.myname)
del s.myname
class member Permissions
There are two types: public members : Private members can be accessed anywhere: within the class, the first two characters are underlined
Private member through object. _ Class name __ variable name can be forced to access
class student: # static variable type = "Human" __sex = "Male" def __init__ (self, id, nam e): # normal variable self.__id = id self.name = Name def show (self): print (Student.type, Studen
T.__sex, self.__id, Self.name) class postgraduate (student): Def Show (self): print (Student.type, self.name) s = Student (3, "Mick") # Parent class S.name S.type s._student__id s._student__sex s.show () # Subclass P = Postgraduate (1, "HWDTC") P.nam E p.type p._student__id p._student__sex p.show ()