What the f**k!! The biggest culprit with this knowledge is that I'm single???
Python Basics (iv):
Object-oriented three features: encapsulation, inheritance, polymorphism
Class:
Objects are the core of object-oriented programming, and in the process of using objects, in order to abstract define a set of objects with common characteristics and behaviors, another new concept-class
The class is equivalent to the drawing of the aircraft, and the aircraft used to create it is equivalent to the object
Class is composed of 3 parts
- Name of class: class name
- Class Properties: A set of data
- Method of the class: Allows the operation of the method (behavior)
Create a class:
class Car (object): # class naming rules according to the big hump nomenclature. Capitalize first letter # Method def Getcarinfo (self): Print (' number of wheels:%d, color%s'% (Self.wheelnum, Self.color))
Add:
Some code classes may not inherit object, and this problem does not exist after Python 3, because all of the classes in Python3 are new classes, and because of the depth and breadth of the problem, interested friends see for themselves
#新式类refers to Class A (object) that inherits object: #经典类refers to classes that do not inherit object class A:
Class method:
Within the class, use the def keyword to define a method that, unlike a generic function definition, must contain the parameter self, and is the first argument, and self represents an instance of the class
# Defining Classes class Car: # Defining Methods # Moving def Move (self): Print (' the car is running ... ' ) # siren def toot (self): Print( " the car is whistling ... Doodle.. ")
__init__ () Method: #init前后符号是两条下划线 _ _ Do not mistake.
Ability to set the object's properties when creating the object
# define Car class car : def __init__(self): self.wheelnum = 4 self.color = ' Blue ' #效果等同于上文的 bmw.color = ' Black ', except for the Add attribute, here is the attribute in the class to write the def Move (self): print (' car in Run, target: Hawaii ') # Create object BMW = car () print (' The car's color is:%s '%bmw.color) print (' Car tires:%d '%bmw.wheelnum)
__init__()method, which is called by default when an object is created, and does not need to be called manually
__init__(self), by default there are 1 parameters whose name is self, and if 2 arguments are passed when the object is created, then the self __init__(self) as the first parameter requires 2 formal parameters, for example__init__(self,x,y)
__init__(self)Does not need to be passed by the developer, the Python interpreter will automatically pass in the current object reference.
Class object:
Class objects support two operations: Property Reference and instantiation
#Defining classes
classCar:#Moving defMove (self):Print('the car is running ...') #Whistle defToot (self):Print("the car is whistling ... Doodle..")#create an object and use the variable BMW to save its referencesBMW =Car () Bmw.color='Black' #给对象添加属性Bmw.wheelnum= 4#number of wheels adding properties to an object Bmw.move () # Call the object's Move Method Bmw.toot () # Call the Toot method of the objectPrint(Bmw.color)Print(Bmw.wheelnum)
--->
The car is running ...
The car is whistling ... Doodle..
Black
4
- BMW = Car (), which produces an instance object of car, which can also be accessed by the instance object BMW to access properties or methods
- The first time you use Bmw.color = ' black ' to add a property to the BMW object, if it appears again Bmw.color = XXX indicates that the property was modified
- BMW is an object that has attributes (data) and methods (functions)
- When an object is created, a mold is used to make a physical
Inheritance of the class:
Subclasses get all the functionality of the parent class
1 #Defining Classes2 classCar:3 #Moving4 defMove (self):5 Print('the car is running ...')6 7 #Whistle8 defToot (self):9 Print("the car is whistling ... Doodle..")Ten One classBMW (Car): A defStop (self): - Print('Stop') - theX5 =BMW () -Audi =Car () - - X5.move () +Audi.move ()
--->
The car is running ...
The car is running ...
Initial knowledge of Python object-oriented