What are classes and objects?
In python, everything is an object, such as a person is an object, a car, a house are Objects.
Each object has its own properties, such as the properties of a Person's object, such as gender, height, age, etc.
The property of the House has price, area, type and so on
in the python , a feature of an object is also known as a property ( attribute ). The behavior it has is also called the Method.
So: Object = attribute + method
In python, we classify objects with the same properties and methods as one class
For example: people, animals, plants, buildings, etc.
A class is a template and blueprint for an object that is an instantiation of a class. A class does not represent a specific thing, but an object represents something Specific.
Note: the property is object-owned, and the class does not have properties for the object!!!
Initialize the Object:
When you create a class, you can define a specific method for __init__ (). This method is run whenever an instance of the class is Created. can provide
The __init__ () method passes a parameter so that you can set the property to the value you want when you instantiate the object
For example:
Person:memo = (, Name,age):. Name=name. Age=agep1=person (,) p1. age,p1. Nameperson.memo
Output results
Laowang
One of the many species
What are static fields and dynamic fields?
What is the self above? What is Name and age? What is name and age?
self is the value that creates the object, which means It represents the object itself, in the example above, Self is p1, and name, age is the property of the object, the field that belongs to the object is called the dynamic field . name, age is the argument that needs to be passed when the object is Created.
What is the memo in the above example? Does it belong to a class or an object?
A memo belongs to a class, and a field that belongs to a class is often referred to as a static field . Self.xx belongs to the Object.
A field that belongs to a class, and static fields are accessed using the class name. field name , for example in the previous example Person.memo
Dynamic fields use the object name. field names , such as P1 in the previous Example. Name, p1. Age
Can a class access dynamic fields? Can objects access static fields?
A class cannot access a dynamic field because a dynamic field is a property of an object and belongs to the object only. objects can access static FIELDS. Access Method: object name. static field name, in the previous example
Print P1.memo One of the many species
Note: to avoid ambiguity, try to avoid object access to static fields
What are static and dynamic methods?
At the very beginning, we said
Object = Property + method
So what is a method?
A method is a behavior of an object, and in Python a method is a function that can be called by an object, and the object invocation method can be written like This: the name of the Object. method name,
For example:
Person: #静态字段 memo = (, name,age): #动态字段. Name=name. Age=age #动态方法 Action ():. Name + Laowang=person (,)//instantiate an object Laoli=person (,) laowang.action ()//object Call method Laoli.action ()
Output result:
Laowang is at work, Laoli is at Work.
In the above example, the action (self) is called a dynamic method, and the dynamic method can only be accessed by the object and cannot be accessed by the class.
With dynamic methods There are also static methods, so what if a static method is defined?
To define a static method, there are two requirements:
1, need to add an adorner "@staticmethod" on the dynamic method
2, remove self, Because static methods belong to the class, and self belongs to the object
To create a static method:
@staticmethoddef foo (): print ' This is a static method '
Person:static (): person.static ()
Output result:
This is a static method
Summary: the difference between static and dynamic methods
Static methods can be accessed by classes and objects, whereas dynamic methods can only be accessed by Objects. Static methods require a @staticmethod decorator and do not have a self
What is an attribute?
features is a definition other than a field and a method that is different from a field and method, usually used to return a value, which is called by an object,
Call Method: Object Name. Method Name Note there is no parenthesis after the method name
What if an attribute is defined?
Add an adorner to the dynamic method @property
For example:
@propertydef Bar (self): return to Self. Name
Person: (, Name,age):. Name=name. Age=age Static (): ():. Namelaowang=person (,) Laoli=person (,) laowang.laoli.
This article is from the "zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1857953
Python object-oriented programming