Python 3.x study notes 9 (Object-Oriented), python3.x
1. Object-oriented
Object-oriented is a method of understanding and abstraction of the real world and a product after the development of computer programming technology to a certain stage.
2. class ):
A class is the abstraction, blueprint, and prototype of a class of objects with the same attributes. Classes define all the attributes of these objects (variables (data) and common methods.
3. Object ):
An object is an instance after a class is instantiated. A class must be instantiated before it can be called in a program. A class can instantiate multiple objects, and each object can have different attributes, just like humans refer to everyone, and each person refers to a specific object. People have similarities and differences with people before.
4. Features
Encapsulation
The value assignment and internal call of data in the class are transparent to external users. This makes the Class A capsule or container, which contains the data and methods of the class.
Inheritance
A class can be derived from a subclass. The attributes and methods defined in this parent class are automatically inherited by the quilt class.
Polymorphism
Polymorphism is an important feature of object orientation. Simply put, "one interface and multiple implementations" means that different subclasses are generated in a base class, each subclass inherits the same method name and implements different methods of the parent class. This is the form of the same thing.
5. Constructor
Initialize some classes during instantiation.
Instance variable: self. a =
Instance variable (static attribute) with the scope of the Instance itself
Example
1 class Person: 2 n = 123 # class variable 3 4 def _ init _ (self, name, ability): 5 # constructor 6 7 self. name = name # instantiate a variable (static attribute) with the scope of 8 self. ability = ability 9 10 def eat (self): # class method functions (dynamic attributes) 11 print ('I will eat something! ') 12 13 def run (self): 14 print (' I will runing! ') 15 16 def sleep (self): 17 print (' I will sleep! ') 18 19 def doing (self): 20 print (' I am % s' % self. ability) 21 22 r1 = Person ('zhang san', 'Dancing ') 23 24 r1.sleep () 25 r1.run () 26 r1.eat () 27 r1.doing () 28 print (r1.n)
Result
I will sleep !I will runing !I will eat something ! I am dancing1234