1, Object oriented overview
1. Overview: object oriented is an important idea in the field of program development. This idea simulates the way of thinking of human beings to understand the objective world and regards all things encountered in development as objects.
2. Object oriented programming has two very important concepts: class and object
(1) A collection of things with the same characteristics and behavior is collectively referred to as a class.
(2) ① objects are created according to classes. A class can correspond to multiple objects.
Instantiation: creating an object from a class is instantiation. An object is an instance of a class, also known as an instantiated object
A class is an abstraction of an object, and an object is an instance of a class.
② Objects really exist in life. Usually, objects are divided into two parts, namely static part and dynamic part.
The static part is called "properties"
The dynamic part refers to the behavior of objects, also known as "methods"
(3) Class consists of three parts:
Class name: generally capitalized, such as Person.
Class attribute: used to describe the characteristics of things, such as gender.
Class method: used to describe the behavior of things, such as leg lifting.
2, Class definition and use
Attribute name = attribute value
def method name (self):
The naming rules of class names shall comply with the big hump naming method. Attribute name = attribute value ， class attribute ， self is required (itself), the first parameter
2. Create object / instantiate object:
Syntax: object name = class name ()
3. Access class properties:
(1) Access class properties by class name
Syntax: class name Attribute name
(2) Access class properties by object name
Syntax: object name Attribute name
4. Modify class properties
(1) Modify class properties by class name (effective for all instantiated objects)
Syntax: class name Property name = "new name"
(2) Modify class properties by object name (only effective for the current instantiated object)
Syntax: object name Property name = "new name"
5. Access the method where the instance property is located -- access / modify the instance property
(1) Access the instance property through the object name to access the method where the instance property is located
(2) Modifying the instantiation property through the object name only takes effect for the current instance object
class Wy: name = "king" sex = "male" age = "20" #method def eat(self): #print("scrambled egg with tomato") #print("fried meat with green pepper") self.food = "fried eggs with tomatoes" #Instantiated object object name = class name () wy1 = Wy() wy2 = Wy() #Access class properties #1. Access class properties by class name print(Wy.name) #2. Access class properties by object name print(wy1.age) #Modify class properties #1. Modifying class properties by class name is effective for all instance objects Wy.age = "18" print(Wy.age) print(wy1.age) #2. Modifying class properties by object name is only effective for the current instance object wy1.name = "Lee" print(wy1.name) # print(wy2.name) has not been modified # print(Wy.name) is not modified #Access instance property through object name access instance property method = = = access / modify instance property wy1.eat() print(wy1.food) #Modifying instance properties takes effect only for the current instance object wy2.eat() wy2.food = "Fried meat with green pepper" print(wy2.food)
6,__ init__ () every time an object is instantiated by the construction method, the method will be executed automatically
Each time an object is created, the function of this method will be automatically executed? Initialize object
7,__ del__ () the destruct method automatically executes every time an object is destroyed
class Students: s_class = "Big data class 2" #The definitions of class attributes are different, and the use is the same #__ init__ () every time an object is instantiated by the construction method, the method will be executed automatically def __init__(self,s_id,name,age,sex): self.s_id=s_id self.name=name self.age=age self.sex=sex print("__init__()Once,objects creating") def sayhi(self): print("I am",self.s_class,"of","My number is:",self.s_id,"My name is:",self.name,"I this year",self.age,"Years old","I am",self.sex,"living") #__ del__ () the destruct method automatically executes every time an object is destroyed def __del__(self): print("The object has been destroyed") s1 = Students("123456","Xiao Wang",18,"male") s1.sayhi() s2 = Students("3690","roy",20,"female") s2.sayhi() #Destroy an object del s2
8. Private members: class members are public members by default
Private members can be represented by double underscores (_) in front of the names of class members
You want to have private properties in the method
Syntax:__ Attribute name
You want to have private methods in your class
Syntax:__ Private method name
class Roy: def __init__(self,name,age): self.name=name self.__age=age def sayhi(self): #1. Return the private variable through the return value return self.__age def __st(self,height,weight): print("My height is:",height,"My weight is:",weight) r1 = Roy("Wang Yuan",18) print(r1.name) #The private property cannot be accessed outside #print(r1.age) print(r1.sayhi()) #2. Private attribute object name_ Class name__ Private property name print(r1._Roy__age) #3. Private method r1._Roy__st(182,100)