I. Creating Classes and objects
Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".
A class is a template that can contain multiple functions and functions in a template.
Objects are instances created from templates that can execute functions in a class through an instance object
class Foo: # Create class + class name def Bar (self): # Create a function in a class Bool=foo () # to create a function from a class
function in a class The first argument must be self, and the function defined in the class is called the "method"
# Create a class
classFoo:defBar (self):Print 'Bar' defHello (self, name):Print 'I am%s'%name#Create object from class Foo objobj =Foo () obj. Bar ()#Execute Bar MethodObj. Hello ('Wupeiqi')#Execute the Hello method
Object-oriented: "Create Object" "Execute method by Object"
Two. Object-oriented three major features
The three major characteristics of object-oriented are: encapsulation, inheritance and polymorphism.
First, the package
Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.
Therefore, when using object-oriented encapsulation features, you need:
Encapsulate content somewhere
To invoke the encapsulated content from somewhere
Self is a formal parameter, when executing obj1 = Foo (' Wupeiqi ', 18), self equals obj1
When executing obj2 = Foo (' Alex ', 78), self equals obj2
So, the content is actually encapsulated in objects Obj1 and OBJ2, each object has a name and an age attribute, which is similar to saving in memory.
Step two: Call the encapsulated content from somewhere
When the encapsulated content is called, there are two scenarios:
Called directly through an object
Indirectly called through self
1. Direct invocation of encapsulated content by object
Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name
classFoo:def __init__(self, Name, age): Self.name=name Self.age=Age Obj1= Foo ('Wupeiqi', 18)PrintObj1.name#call the Name property of the Obj1 object directlyPrintObj1.age#call the Age property of the Obj1 object directlyObj2= Foo ('Alex', 73)PrintObj2.name#call the Name property of the Obj2 object directlyPrintObj2.age#call the Age property of the Obj2 object directly
2. Indirectly invoking the encapsulated content via self
When executing a method in a class, the encapsulated content needs to be indirectly called through self
classFoo:def __init__(self, Name, age): Self.name=name Self.age= Agedefdetail (self):PrintSelf.namePrintself.age obj1= Foo ('Wupeiqi', 18) Obj1.detail ()#Python will pass obj1 to the self parameter by default, which is: Obj1.detail (obj1), so the self = Obj1 inside the method, that is, Self.name is Wupeiqi; Self.age isObj2= Foo ('Alex', 73) Obj2.detail ()#Python will pass obj2 to the self parameter by default, namely: Obj1.detail (OBJ2), so the self = Obj2 inside the method, that is: Self.name is Alex; Self.age is 78
Internal self = obj2, that is: Self.name is Alex; Self.age is 78.
Ii. inheritance
Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content.
For example:
Cats can: Meow meow, eat, drink, pull, sprinkle
Dogs can: bark, eat, drink, pull, sprinkle
If we were to create a class for both cats and dogs, it would be necessary to implement all of their functions for cats and dogs, such as the following
classCat:defmeow meow (self):Print 'Meow Meow' defEat (self):#Do something defDrink (self):#Do something defPull (self):#Do something defScatter (self):#Do somethingclassDog:defBarking (self):Print 'Meow Meow' defEat (self):#Do something defDrink (self):#Do something defPull (self):#Do something defScatter (self):#Do something
The above code is not difficult to see, eat, drink, pull, and Satan is the function of both cats and dogs, and we are the cat and the dog's class has been written two times. If you use the idea of inheritance, implement as follows:
Animals: Eating, drinking, pulling, spreading
Cat: Meow Meow (cat inherits function of animal)
Dog: Barking (dogs inherit the function of animals
classAnimal:defEat (self):Print "%s Eat"%Self.namedefDrink (self):Print "%s Drink"%Self.namedefshit (self):Print "%s Pull"%Self.namedefpee (self):Print "%s Caesar"%Self.nameclassCat (Animal):def __init__(self, name): Self.name=name self.breed ='Cat' defCry (self):Print 'Meow Meow'classDog (Animal):def __init__(self, name): Self.name=name self.breed ='Dog' defCry (self):Print 'Barking' ########## Execution #########C1= Cat ('Small White House of Little black Cat') c1.eat () C2= Cat ('Little Black Little white cat') C2.drink () D1= Dog ('Fat family's little skinny dog') D1.eat () Code instance
Day7 Python Object-oriented