#__author:"吉" #date: 2018/10/27 0027 #function: # 设计类: ‘‘‘ 1 类名:首字母大写,见名思意 2 属性:驼峰原则 3 行为:见名思意,驼峰法 说明:类不占空间,实例化对象占用空间! ‘‘‘ # 格式,object是父类,超类 ‘‘‘ 类名(object): 属性 行为 ‘‘‘ class Peoson(object): name = ‘zhanglei‘ age = 24 weight = 70 def run(self): print("跑!") def eat(self): print(‘吃‘)
#__author:"吉" #date: 2018/10/27 0027 #function: # 设计类: ‘‘‘ 1 类名:首字母大写,见名思意 2 属性:驼峰原则 3 行为:见名思意,驼峰法 说明:类不占空间,实例化对象占用空间! ‘‘‘ # 格式,object是父类,超类 ‘‘‘ 类名(object): 属性 行为 ‘‘‘ class Peoson(object): name = ‘zhanglei‘ age = 24 weight = 70 def run(self): print("跑!") def eat(self): print(‘吃‘) def changeName(self,name): self.name = name # 实例化对象 ‘‘‘ 格式:对象名= 类名(参数列表信息) ‘‘‘ peoson1 = Peoson() print(peoson1.name,peoson1.age,peoson1.weight) print(peoson1.eat()) print(peoson1.run()) # 原理 ‘‘‘ 变量是在栈区,对象是在堆区。 ‘‘‘
#__author:"吉" #date: 2018/10/27 0027 #function: # 设计类: ‘‘‘ 1 类名:首字母大写,见名思意 2 属性:驼峰原则 3 行为:见名思意,驼峰法 说明:类不占空间,实例化对象占用空间! ‘‘‘ # 格式,object是父类,超类 ‘‘‘ 类名(object): 属性 行为 ‘‘‘ class Peoson(object): name = ‘zhanglei‘ age = 24 weight = 70 def run(self): print("跑!") def eat(self): print(‘吃‘) def changeName(self,name): self.name = name # 实例化对象 ‘‘‘ 格式:对象名= 类名(参数列表信息) ‘‘‘ peoson1 = Peoson() # 访问属性 ‘‘‘ 变量是在栈区,对象是在堆区。 ‘‘‘ print(peoson1.name,peoson1.age,peoson1.weight) peoson1.name = ‘jiji‘ peoson1.age = 33 peoson1.weight = 90 print(peoson1.name,peoson1.age,peoson1.weight) peoson1.changeName(‘lala‘) print(peoson1.name,peoson1.age,peoson1.weight)
#__author:"吉勇佳" #date: 2018/10/27 0027 #function: ‘‘‘ 构造函数:__init__() 是在创建类的时候自动调用,不写出这个 构造函数的话,默认是一个空的构造函数什么页不执行。 ‘‘‘ class Peoson(object): def __init__(self,name,age,height,weight): self.name = name self.age = age self.height = height self.weight = weight def run(self): print("跑!") def eat(self): print(‘吃‘) def changeName(self,name): self.name = name # 实例化对象 p1 = Peoson("jiyongjia",24,177,78) print(p1.name,p1.age,p1.height,p1.weight) p1.changeName(‘zhanglei‘) print(p1.name,p1.age,p1.height,p1.weight) # self 原理 ‘‘‘ 1、哪个对象调用,self就代表那个对象。 ‘‘‘
class Peoson(object): def __init__(self,name,age,height,weight): self.name = name self.age = age self.height = height self.weight = weight # 这里是析构函数 def __del__(self): print("我是析构函数") def run(self): print("跑!") def eat(self): print(‘吃‘) def changeName(self,name): self.name = name # 创建对象函数 self.__class__ 是代表类名的 def createObj(self,name): p=self.__class__(name,24,56,89) print(p.name,p.age,p.weight,p.height) # 即 执行p1的一个方法即可创建新的对象。 p1 = Peoson("丽丽",33,53,222) print(p1.name,p1.age,p1.height,p1.weight) p1.createObj("狗熊") ‘‘‘ 输出:丽丽 33 53 222 狗熊 24 89 56 我是析构函数 我是析构函数 ‘‘‘ print(p1.name)
‘‘‘ 重写 __str__() str方法是给用户用的,用于返回用户需要的结果信息、 __repr__() 如果换成__repr__()是得到与str相同的结果。是在黑屏终端直接敲对象名再回车的方法 注意:在没有str方法但是有repr的时候,repr 就相当于str,只是repr用于黑屏终端 ‘‘‘ class Peoson(object): def __init__(self,name,age,height,weight): self.name = name self.age = age self.height = height self.weight = weight # 这里是析构函数 def __del__(self): print("我是析构函数") def run(self): print("跑!") def eat(self): print(‘吃‘) def changeName(self,name): self.name = name # 创建对象函数 self.__class__ 是代表类名的 def createObj(self,name): p=self.__class__(name,24,56,89) print(p.name,p.age,p.weight,p.height) # 如果换成__repr__()是得到相同的结果。是在黑屏终端直接敲对象名再回车的方法 def __str__(self): pass # return "这里是str" # return self.name #返回多个值的话用如下 return "%s-%d-%d" % (self.name,self.age,self.weight) # 如果要打印出所有的属性信息呢? p1 = Peoson("嘉嘉",44,222,336) # 不写def __str__()方法的时候,打印出的是该对象的地址信息 print(p1) ‘‘‘ 输出:<__main__.Peoson object at 0x0000000002564978> ‘‘‘ # 写了__str__()函数是 打印出的自己需要返回的信息数据,此时p1等价于p1.__str__() print(p1)
#__author:"吉" #date: 2018/10/27 0027 #function: # 枪设计子弹 设计一次少一个子弹,没子弹了提示无法射击 class Gun(object): def __init__(self,name,bulletNum): self.name = name self.bulletNum = bulletNum def shot(self): self.bulletNum =self.bulletNum - 1 if self.bulletNum+1 != 0: print("此枪型号是:%s,此次射击成功!子弹减少1发,余量为:%d" %(self.name,self.bulletNum)) else: print("*************射击失败,子弹数为0.已经为您子弹加弹5发!请重新射击!**************") self.bulletNum = 5 gun = Gun("AK47",5) # 射击敌人 gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() gun.shot() ‘‘‘ 输出: 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:4 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:3 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:2 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:1 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:0 *************射击失败,子弹数为0.已经为您子弹加弹5发!请重新射击!************** 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:4 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:3 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:2 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:1 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:0 *************射击失败,子弹数为0.已经为您子弹加弹5发!请重新射击!************** 此枪型号是:AK47,此次射击成功!子弹减少1发,余量为:4 ‘‘‘
原文:https://www.cnblogs.com/jiyongjia/p/9863949.html