Object-oriented primary knowledge
Class Concept: A category of things with the same attributes and skills
The concept of an object: a specific description of a class
Use object-oriented benefits:
1. Make the role relationships between the code more explicit
2. Enhanced Code Extensibility
3. Specification of object properties and performance
Object-oriented features:
The uncertainty of the outcome
Description and structure of the internal parts of the class:
Class name (first capitalization):
#引用静态变量
#1. Class name. __dict__[' static variable name '] #可以查看, but cannot be censored
#2. class name. Static variable name #直接就可以访问, can be censored
#删除一个静态变量:d El class name. Static variable Name
#引用动态变量
#1. Class name. Method Name #查看这个方法的内存地址
#2. Class name. Method Name (argument) #调用了这个方法, you must pass an argument to the self
#创造一个对象-Example of
Process of #产生一个实例 (object)
#格式: Object = class name ()
Here is a simple class that corresponds to the concept above:
classPerson:role=' Person' def __init__(SELF,NAME,SEX,HP,AD): Self.name=name Self.sex=Sex self.hp=HP Self.ad=ADdefAttack (self):Print('an attack was initiated by%s'%self.name) Alex= Person ('A_SB','Unknown', 1,5) Boss_jin= Person ('Kim boss','female', 20,50)
The process of instantiating in a class:
1. Create an instance that will act as an actual parameter
2. Automatically triggers a __list__ method and passes the instance as a reference to the self parameter in the __init__ method
3. After the __init__ method is executed, self is automatically returned to Alex
__init__ Method: Initialize the method, add some basic properties to an object method, the general situation is not the assignment of self
Object:
1. Inside of the class self is an object of Ben
2. Outside of the class, each object corresponds to a name that points to the memory space of an object
Invocation of the property: (Two invocation methods)
1. Object name. Property name
2. Object name. __dict__[' property name ']
Method invocation: (Two ways)
1. Class name. Method Name (object name) # then the self parameter in the method points to the object
2. Object name. Method Name () # This is equivalent to the self parameter in the method that points directly to the object
Object-oriented parameter and interactive output
classPerson:role=' Person' def __init__(SELF,NAME,SEX,HP,AD): Self.name=name Self.sex=Sex self.hp=HP Self.ad=ADdefAttack (Self,user): user.hp-=Self.adPrint('%s launched an attack%s,%s lost the blood from%s .'%(self.name,user.name,user.name,self.ad))classDog:def __init__(SELF,NAME,KIND,HP,AD): Self.name=name Self.sex=Kind SELF.HP=HP Self.ad=ADdefBite (Self,ani):Print('%s launched an attack%s,%s lost the blood from%s .'%(self.name,ani.name,ani.name,self.ad)) ani.hp-=self.ad Tom= Person ('Tamworth','male', 60,45) Teddy= Dog ('stupid , stupid.','Teddy', 50,10) Tom.attack (Teddy) Teddy.bite (Tom)Print(TOM.HP)Print(TEDDY.HP)
Results:
Tamworth launched an attack, stupid, dumb, stupid, 45-point blood, stupid, a attack Tamworth, Tamworth lost 10 points of blood 505
Namespaces and Scopes
Summarize:
Object properties are unique, and static properties and methods are shared
The object uses the name: first find its own memory space, and then find the class of memory space in the
Class name. Static variable name: For static property modification, the class name should be modified directly
Example code:
Show how much money is there now?
In this example, money is an immutable data type, and Self.money is equivalent to creating a property called Money in the object memory space and cannot reference \
Static variables in the class memory space
classPerson:money= 0def __init__(self,name): Self.name=namedefWork (self):Print(Self.name,'work, earn 1000 bucks.') Self.money+ = 1000Father= Person ('Father') Mother= Person ('Mother') Mother.work () father.work ()Print(Person.money)
becomes a mutable data type, it is possible to index a reference to money, because it is a mutable type, object space cannot be created, only the money of the class space can be referenced
classPerson:money=[0]def __init__(self,name): Self.name=namedefWork (self):Print(Self.name,'work, earn 1000 bucks.') self.money[0]+ = 1000Father= Person ('Father') Mother= Person ('Mother') Mother.work () father.work ()Print(Person.money)
Object-oriented combination usage
The important way of software reuse in addition to inheritance there is another way, namely: the combination
A combination refers to a class in which an object of another class is used as a data property, called a combination of classes
The following example:
classPerson :def __init__(SELF,NAME,SEX,HP,AD): Self.name=name Self.sex=Sex self.hp=HP Self.ad=AD Self.money=0defAttack (Self.dog): dog.hp-=Self.adPrint(%s attacked%s,%s lost the blood of%s.'% (self.name,d.name,d.name,self.ad)) defPay (self): money= Int (Input ('Please enter the amount you want to recharge:')) Self.money+= MoneyPrint("balance is:%s"%Self.money)defWear (self,weapon):#weapon as a reference to the weapon class ifSelf.money >=Weapon.price Self.weapon= Weapon#The combination gives the man a weapon.Self.money-=Weapon.pricePrint('successful purchase, you have successfully equipped the%s'%weapon.name)Else: Print('insufficient balance, please recharge') defAttack_with_weapon (self,dog):if 'Weapon' inchself._dict_: Self.weapon.skill (dog)Else: Print('Please equip your weapon first .')classDog:def __init__(SELF,NAME,KIND,HP,AD): Self.name=name Self.kind=Kind SELF.HP=HP Self.ad=ADdefbit (self,p): p.hp-=Self.adPrint('%s took a bite of%s and%s lost the blood of%s.'%(Self.name, P.name, P.name, self.ad))classWeapon:def __init__(self,name,price,ad,level): Self.name=name Self.price=Price Self.level=Level Self.ad= AD *Self.leveldefSkill (self,dog): dog.hp-=Self.adPrint('%s has been damaged by%s ,%s has lost the blood of %s'% (Dog.name,self.name,dog.name,self.ad))
Example 2, by the calculation of the cylinder to achieve the combination of classes
fromMathImportPiclassin_circle:def __init__(self,r): SELF.R=RdefArea (self):returnPI*SELF.R * * 2defperimeter (self):returnPI*SELF.R * 2classRing:def __init__(self,big_r,small_r): Self.big_r=in_circle (big_r) #一个类中以另外一个类的对象作为属性 Self.small_r=in_circle (Mall_r)#一个类中以另外一个类的对象作为属性
def n_area (self):
return Self.big_r.area-self.small_r.area
def N_perimeter (self):
return Self.big_r.perimeter + self.small_r.perimeter
= Ring (8,3
Print
Print (R2.n_perimeter ())
Python Foundation 16th---19 days (object-oriented first knowledge)