object
1. An object is a concrete existence created by a class and can be used directly
2. The object created by which class has the properties and methods defined in which class
3. The object is relative to the aircraft manufactured by the application drawing, and the drawing is a class
4. In development, there should be classes first and then objects
5. The object defined by the class has any attribute. The object has no more or less attributes
6. Class name: the name of such things, which meets the naming method of big hump
Naming method of the big hump: the first letter of each word is capitalized, and there is no underline between words
7. Attribute: what are the characteristics of such things
8. Method: what behavior does this kind of thing have
Define a class that contains only methods
class name:
def method 1 (self, parameter list)
pass
def method 2 (self, parameter list)
pass
The definition of a method is almost the same as that of a function, except that there is a self
The class name shall conform to the big hump nomenclature
create object
Object variable = class name ()
class Cat: def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") tom=Cat() tom.eat() tom.drink()
The result is
The cat wants to eat fish
The cat needs water
If you add an attribute to an object, the object name. Attribute name = content
class Cat: def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") tom=Cat() tom.name="Big lazy cat" print(tom.name)
The result is: big lazy cat
However, it is not recommended to define new attributes outside the class, because if the attribute is not found during operation, an error will be reported, so the attribute should be encapsulated in the class
init()
The initialization method is the * * init() * * method, which is also a built-in method. Init() specifically defines the attributes of the class
In__ init__ Method can define an attribute by using self. Attribute name = initial value of the attribute
After defining the attribute, all objects created with cat will have the attribute
class Cat: def __init__(self,name): self.name=name def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") tom=Cat("tom") print(tom.name) layer_cat=Cat("Big lazy cat") print(layer_cat.name)
The result is:
tom
Big lazy cat
del()
It is also a built-in function, which is called when the object is executed.
class Cat: def __init__(self,name): self.name=name def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") def __del__(self): print(f'{self.name}be gone') tom=Cat("tom") lanmao=Cat("Lazy cat") print(tom.name) print(lanmao.name) print(tom.name)
The result is:
tom
Lazy cat
tom
tom's gone
The lazy cat is gone
class Cat: def __init__(self,name): self.name=name def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") def __del__(self): print(f'{self.name}be gone') tom=Cat("tom") lanmao=Cat("Lazy cat") print("--------------------------")
The result is:
'------------------------------------------------'
tom's gone
The lazy cat is gone
class Cat: def __init__(self,name): self.name=name def eat(self): print("The cat wants to eat fish") def drink(self): print("The cat needs water") def __del__(self): print(f'{self.name}be gone') tom=Cat("tom") lanmao=Cat("Lazy cat") del tom print("--------------------------")
The result is:
tom's gone
'-------------------------------------------'
The lazy cat is gone
del tom is the deleted object. If the deleted attribute is called under this line of code, an error will be reported
str()
Returns the description of an object class cat(): def __init__(self,name): self.name=name def __str__(self): return f"The cat's name is{self.name}" #Returns the description of the object def eat(self): print(f"My cat loves to eat") tom=cat("tom") print(tom)
The result is:
The cat's name is tom
Exercise 1
Create house class and furniture class with class, and analyze the attributes of furniture as furniture name and floor area Analyze the properties of the house, house style and area, and add furniture class jiaju(): def __init__(self,name,area): self.name=name self.area=area def __str__(self): return f"What is the name of the furniture{self.name}What is the floor area of furniture{self.area}" led=jiaju("lamp",4) print(led) cabinet=jiaju("cabinet",8) print(cabinet) class house(): def __init__(self,house_style,area): self.house_style=house_style self.area=area self.free_area=area self.jiaju_list=[] def __str__(self): return f"House type is{self.house_style}\t The total area is{self.area}\t Furniture has{self.jiaju_list}\t The remaining area is{self.free_area}" def adress_jiaju(self,item): if item.area>self.free_area: print("The furniture area is larger than the remaining area, so it can't be put down") return else: self.jiaju_list.append(item.name) self.free_area=self.free_area-item.area house1=house("villa",220) print(house1) house1.adress_jiaju(led) print(house1) house1.adress_jiaju(cabinet) print(house1)
The result is:
The name of the furniture is lamp. The floor area of the furniture is 4
The name of the furniture is cabinet. The floor area of the furniture is 8
The house type is a villa, with a total area of 220, furniture [] and a remaining area of 220
The house type is a villa, with a total area of 220, furniture ['lights'] and a remaining area of 216
The house type is a villa, with a total area of 220, furniture ['lights',' cabinets'] and a remaining area of 208
Exercise 2
class Gun(): def __init__(self, gun_name, sum): self.gun_name = gun_name self.sum = sum def __str__(self): return f"This weapon is called{self.gun_name}\t Now there are{self.sum}Bullet" def ardess_sum(self, adress_zidanshumu): # Method of adding bullets to gun if self.sum == 100: # Suppose the gun can hold 100 bullets return elif self.sum + adress_zidanshumu > 100: print("The number of bullets pressed exceeds the capacity of the magazine") else: self.sum += adress_zidanshumu print(f'Bullets added successfully. Now there are{self.sum}Bullet') def fashe(self, fashe_sum): # Method of firing bullets from a gun for i in range(fashe_sum): if self.sum > 0: print("Da") self.sum -= 1 else: print("There are no bullets") break class soldier(): def __init__(self, name,weapons): self.name = name self.weapons = weapons def __str__(self): return f"My name is{self.name}My weapon is{self.weapons}" def fire(self): if self.weapons.sum > 0: print("I can charge") b=int(input("Do you want to add bullet 1------Add, 2-----Do not add")) if b==1: c=int(input("Please enter the number of bullets you want to add")) self.weapons.ardess_sum(c) print(f"Now?{self.weapons.gun_name}There are{self.weapons.sum}Bullet") a = int(input("How many shots are you going to shoot")) self.weapons.fashe(a) else: a = int(input("How many shots are you going to shoot")) self.weapons.fashe(a) AK47 = Gun("ak47", 20) print(AK47) AK47.ardess_sum(10) AK47.fashe(2) xusanduo = soldier("Xu Sanduo",AK47) print(xusanduo) xusanduo.fire()
Private properties and private methods
Private properties are properties that an object does not want to expose
A private method is a method that an object does not want to expose
When defining a method or attribute, add two before the attribute name or method name, Means to define a private property or private method
class women(): def __init__(self,name): self.name=name self.__age=18 def mimi(self): #Private properties can be called inside a method print(f"Her name is{self.name}What is her age{self.__age}") xiaofang=women("Xiao Fang") xiaofang.mimi() #Private properties cannot be called externally print(xiaofang.age)
The result is:
Her name is Xiao Fang and her age is 18
Traceback (most recent call last):
File "C:/Users/py/PycharmProjects/pythonProject/new2.py", line 9, in
print(xiaofang.age)
AttributeError: 'women' object has no attribute 'age'
class women(): def __init__(self,name): self.name=name self.__age=18 def __mimi(self): #Private properties can be called inside a method print(f"Her name is{self.name}What is her age{self.__age}") xiaofang=women("Xiao Fang") xiaofang.__mimi()
The result is:
Traceback (most recent call last):
File "C:/Users/py/PycharmProjects/pythonProject/new2.py", line 9, in
xiaofang.__mimi()
AttributeError: 'women' object has no attribute '__mimi'
An underscore plus class name plus method name can access private methods or properties
class women(): def __init__(self,name): self.name=name self.__age=18 def __mimi(self): #Private properties can be called inside a method print(f"Her name is{self.name}What is her age{self.__age}") xiaofang=women("Xiao Fang") xiaofang._women__mimi() print(xiaofang._women__age)
The result is:
Her name is Xiao Fang and her age is 18
18