First, object-oriented knowledge
Structurally understood: Object-oriented is divided into two parts
Class A: name = ' Alex ' # static property, static variable, static field. def func1 (self): # Function, dynamic property, method Pass
function vs Object-oriented:
- Functions encapsulate a function, and object-oriented encapsulates several related functions.
- Object-oriented abstraction, which is an idea that stands in the perspective of God to understand it.
- The program is extensible and the objects are independent. Coupling, difference.
def Ragister (argv): Pass def Login (argv): Pass def Shoppingcar (argv): Pass
function
class Shopping_car: def __init__ (self): # Special Method pass def ragister (self): Pass def login (self): pass def Shoppingcar (self): Pass
Object Oriented
Classes and objects:
Class: A kind of object with the same property or function
Object: An object is a concrete embodiment of a class.
Second, the use of class names and objects
First, define a class first.
# A public framework, a public model class Person: animal = ' Advanced animal ' Walk_way = ' upright walking ' language = ' language ' def eat (self): print (' Eat ') def work (self): print (' working ')
Use of the class name:
- View all properties and methods in a class __dict__
- View, (Add and remove) A class, some property, a method, with a universal point "."
- Operation method, generally not by the class name operation
1 #a public framework, a public model2 classPerson :3Animal ='Advanced Animals'4Walk_way ='Upright Walking'5Language ='language'6 7 defEat (self):8 Print('Eat')9 Ten defWork (self): One Print('work') A - #class name - the #1. View all the properties and methods in the class __dict__ - - Print(person.__dict__) - Print(person.__dict__['Animal'])#through the __dict__ method of the individual properties and methods can be checked but cannot be deleted and modified + #At work, the general use of __dict__ in learning is to look at all the properties and methods in a class, without doing anything else. - + #2. View, (Add and remove) A class, some property, a method, with a universal point. A Print(Person.animal)#Check at Print(person.language) -Person.name ='Alex' #Increase - Print(Person.name) -Person.animal ='low-grade animals' #change. Although the above has not changed, but the memory level has been changed - Print(Person.animal) - #del Person.walk_way # Delete in - #3. Operation methods generally do not operate by the class name to #person.__dict__[' work '] (888) # __DICT__ execution method not recommended +Person.work (999) -Person.eat (999)use of class names
Use of objects:
There are three stages within the instantiation process:
- Opens up an object space in memory
- The __init__ method in the class is automatically executed, and the object space is passed to the self parameter, and other parameters are manually passed in
- Executes the __init__ method, encapsulating the corresponding property for the object space.
1 classPerson :2Animal ='Advanced Animals'3Walk_way ='Upright Walking'4Language ='language'5 6 def __init__(self,name,age):#Features: Encapsulating attributes for Objects7 Print(self)#Self = the person () address is the same8 Print(666)9Self.name1 =nameTenSelf.age1 = Age One A defEat (self): - Print('Eat') - the defWork (self): - Print('work') - - +obj = person ('Alex', 37)#This process is an instantiation process that instantiates an object. (It instantiates an object space in memory) - Print(obj) + Print(OBJ.NAME1)instantiation Process
1 classPerson :2Animal ='Advanced Animals'3Walk_way ='Upright Walking'4Language ='language'5 6 def __init__(self,name,age):#Features: Encapsulating attributes for Objects7 Print(self)#Self = the person () address is the same8 Print(666)9Self.name1 =nameTenSelf.age1 = Age One A defEat (self): - Print('Eat') - the defWork (self): - Print('work') - -obj = person ('Alex', 37) + #object: - #object Manipulation Object Space + #object to view all the properties of the object space __dict__ A Print(obj.__dict__) at #a property of an object manipulation object, adding and removing changes to the universal point. -Obj.sex ='nan' #Increase - Print(obj.__dict__) - #del obj.sex # Delete -Obj.age1 = 99#Change - Print(obj.__dict__) in Print(OBJ.NAME1)#Check - to #the properties of the object manipulation class space can only be checked + Print(Obj.animal) - the * #methods for manipulating class spaces with objects $Obj.eat ()use of ObjectsThird, class namespace, object namespace
1. Why can an object invoke properties and methods in a class?
Obj. property names are first searched from their own space, without this property it will be found through the class object pointer from the class, the class can not be found, will be found from the parent class.
An object can only view properties in a class, and cannot delete or remove attributes from a class.
2. Can a class invoke the properties of an object?
No
3. Can the object and object not be called to each other?
Objects that are instantiated from the same class are not accessible to each other.
However, instantiated objects formed by different classes may have access to each other.
Class and object space:
classGamerole:rule='Game Rules' def __init__(SELF,AREA,NICKNAME,HP,AD): Self.area=Area Self.nickname=Nickname SELF.HP=HP Self.ad=ADdefAttack (self):Print('who has cast an attack') Gailun= Gamerole ('Demacia','Bush LUN', 1000, 75) Yasuo= Gamerole ('Ayonia','Nursery', 500, 150)#1. Why can an object invoke properties and methods in a class#obj. property names are first searched from their own space, without this property it will be found through the class object pointer from the class, the class can not be found, will be found from the parent class. Print(GAILUN.HP) Gailun.attack= 666#adding properties to an objectPrint(Gailun.attack)#666Gailun.rule=Gailun.rulePrint(Gailun.rule)#Game RulesGailun.nickname ='Galen'Print(Gailun.nickname)#object can only view properties in a class, cannot delete or remove attributes from a class#2. Can a class invoke the properties of an object? Can't#print (Gamerole.area) cannot#3. Can I call each other between objects and objects?#objects that are instantiated from the same class are not accessible to each other. #However, instantiated objects formed by different classes may have access to each other. #Encapsulating attributes for objects: __init__, AnywhereView Code
Object-oriented Python