类的语法
1.1语法
1 class Dog(object): # 定义一个类
2 def __init__(self, name): # 构造函数,初始换对象
3 self.NAME = name
4
5 def sayhai(self):
6 print("hello,I am dog my name is", self.NAME)
7
8 d = Dog('Lily') # 创建一个实例 Dog(d,‘Lily')
9 d.sayhai()
1.2self关键字
self关键字相当于实例化后的实例本身d,在实例化过程中,把自己传进去
类方法
1.1封装
封装最好理解了。封装是面向对象的特征之一,是对象和类概念的主要特性。封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。
class Role(object): # 定义一个类 def __init__(self, name, role, weapon, life_value= 888, money= 666666): # 构造函数,初始换对象 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.money = money self.__heart = "live" # 定义一个私有属性,只能在内部访问 def shot(self): print("%s is shotting"%(self.name)) self.__heart = "die" print(self.__heart) def got_shot(self): print("%s is got_shot"%(self.name)) def buy_gun(self,gun_name): print("%s is buy_gun"%(self.name)) r1 = Role('Angle', 'police', 'AK47') r2 = Role('Hebe', 'police', 'AK47') r1.shot() 结果: Angle is shotting die
以上的私有属性只能在内部访问,外部是无法访问的,要想外部能够访问(只能查看,不能修改),需另外另一个方法属性 或者强制访问,强制访问格式:对象名._类名__属性名
1 class Role(object): # 定义一个类 2 def __init__(self, name, role, weapon, life_value= 888, money= 666666): # 构造函数,初始换对象 3 self.name = name 4 self.role = role 5 self.weapon = weapon 6 self.life_value = life_value 7 self.money = money 8 self.__heart = "live" # 定义一个私有属性,只能在内部访问 9 10 def shot(self): 11 print("%s is shotting"%(self.name)) 12 self.__heart = "die" 13 print(self.__heart) 14 def get_heart(self): 15 return self.__heart #对外部提供只读访问接口,但不能修改 16 17 def got_shot(self): 18 print("%s is got_shot"%(self.name)) 19 20 def buy_gun(self,gun_name): 21 print("%s is buy_gun"%(self.name)) 22 23 24 r1 = Role('Angle', 'police', 'AK47') 25 r2 = Role('Hebe', 'police', 'AK47') 26 r1.shot() 27 print(r1.get_heart()) 28 print(r1._Role__heart) # 强制访问私有属性 对象名._类名__属性名
公有属性
能够被所有实例都调用的属性称为公有属性,可以提供这个类所属的所有对象都可以访问的属性。公有属性直接在类里定义
1 class Role(object): # 定义一个类 2 nationality= "AB" # 在类里直接定义的属性为公有属性 3 def __init__(self, name, role, weapon, life_value= 888, money= 666666): # 构造函数,初始换对象 4 self.name = name 5 self.role = role 6 self.weapon = weapon 7 self.life_value = life_value 8 self.money = money 9 self.__heart = "live" # 定义一个私有属性,只能在内部访问 10 11 def shot(self): 12 print("%s is shotting"%(self.name)) 13 self.__heart = "die" 14 print(self.__heart) 15 def get_heart(self): 16 return self.__heart #对外部提供只读访问接口,但不能修改 17 18 def got_shot(self): 19 print("%s is got_shot"%(self.name)) 20 21 def buy_gun(self,gun_name): 22 print("%s is buy_gun"%(self.name)) 23 24 25 r1 = Role('Angle', 'police', 'AK47') 26 r2 = Role('Hebe', 'police', 'AK47') 27 print(r1.nationality) 28 print(r2.nationality) 29 Role.nationality = "CN" 30 print("after change".center(50, "*")) 31 print(r1.nationality) 32 print(r2.nationality) 33 r1.nationality = "USA" 34 print("after change".center(50, "*")) 35 print(r1.nationality) 36 print(r2.nationality)
结果: AB AB *******************after change******************* CN CN *******************after change******************* USA CN
析构函数
定义:在实例销毁的时候调用的函数
调用说明:
其实每一个对象都是一个应用,就像每一个房间都有门牌号一样, 只要这个对象的引用被清空时,就会自动执行,就像如下del r1,其实python中有自动垃圾回收机制,会定时去的去回收一些被清空的应用,而析构函数就是在引用被清空之后会自动执行
class Role(object): # 定义一个类 nationality= "AB" # 在类里直接定义的属性为公有属性 def __init__(self, name, role, weapon, life_value= 888, money= 666666): # 构造函数,初始换对象 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.money = money self.__heart = "live" # 定义一个私有属性,只能在内部访问 def shot(self): print("%s is shotting"%(self.name)) self.__heart = "die" print(self.__heart) def get_heart(self): return self.__heart #对外部提供只读访问接口,但不能修改 def got_shot(self): print("%s is got_shot"%(self.name)) def buy_gun(self,gun_name): print("%s is buy_gun"%(self.name)) def __del__(self): print("删除类对象后,此函数自动执行") r1 = Role("cc","IT","R333") del r1