class People(object): country = 'china' #类方法,用classmethod来进行修饰 @classmethod def getCountry(cls): return cls.country p =People() #可以用过实例对象引用 print People.getCountry() #可以通过类对象引用 print(p.getCountry())
class People(object): country = 'china' #类方法,用classmethod来进行修饰 @classmethod def getCountry(self): return self.country @classmethod def setCountry(self,country): self.country = country p=People() #可以用过实例对象引用 print People.getCountry() #可以通过类对象引用 print(p.getCountry()) print(People.getCountry()) p.setCountry('USA') print(p.getCountry())
#英雄 class Ren: def __init__(self,name): self.name = name self.xue = 100 self.qiang = None def __str__(self): return self.name "剩余血量为:" str(self.xue) def anzidan(self,danjia,zidan): danjia.baocunzidan(zidan) def andanjia(self,qiang,danjia): qiang.lianjiedanjia(danjia) def naqiang(self,qiang): self.qiang = qiang def kaiqiang(self,diren): self.qiang.she(diren) def diaoxue(self,shashangli): self.xue -= shashangli #弹夹类 class Danjia: def __init__(self, rongliang): self.rongliang = rongliang self.rongnaList = [] def __str__(self): return "弹夹当前的子弹数量为:" str(len(self.rongnaList)) "/" str(self.rongliang) def baocunzidan(self,zidan): if len(self.rongnaList) < self.rongliang: self.rongnaList.append(zidan) def chuzidan(self): #判断当前弹夹中是否还有子弹 if len(self.rongnaList) > 0: #获取最后压入到单间中的子弹 zidan = self.rongnaList[-1] self.rongnaList.pop() return zidan else: return None #子弹类 class Zidan: def __init__(self,shashangli): self.shashangli = shashangli def shanghai(self,diren): diren.diaoxue(self.shashangli) #枪类 class Qiang: def __init__(self): self.danjia = None def __str__(self): if self.danjia: return "枪当前有弹夹" else: return "枪没有弹夹" def lianjiedanjia(self,danjia): if not self.danjia: self.danjia = danjia def she(self,diren): zidan = self.danjia.chuzidan() if zidan: zidan.shanghai(diren) else: print("没有子弹了,放了空枪....") #创建一个英雄对象 heroWang = Ren("王英雄") #创建一个弹夹 danjia = Danjia(20) print(danjia) #循环的方式创建一颗子弹,然后让老王把这颗子弹压入到弹夹中 i=0 while i<5: zidan = Zidan(5) heroWang.anzidan(danjia,zidan) i =1 #测试一下,安装完子弹后,弹夹中的信息 print(danjia) #创建一个枪对象 qiang = Qiang() print(qiang) #让老王,把弹夹连接到枪中 heroWang.andanjia(qiang,danjia) print(qiang) #创建一个敌人 diren = Ren("敌人") print(diren) #让王拿起枪 heroWang.naqiang(qiang) #王开枪射敌人 heroWang.kaiqiang(diren) print(diren) print(danjia)
class 类名: 方法列表
# 定义类 class Car: # 方法 def getCarInfo(self): print('车轮子个数:%d, 颜色%s'%(self.wheelNum, self.color)) def move(self): print("车正在移动...")
# 定义一个类 class Animal: # 方法 def __init__(self, name): self.name = name def printName(self): print('名字为:%s'%self.name) # 定义一个函数 def myPrint(animal): animal.printName() dog1 = Animal('西西') myPrint(dog1) dog2 = Animal('北北') myPrint(dog2)
class People(object): def __init__(self, name): self.__name = name def getName(self): return self.__name def setName(self, newName): if len(newName) >= 5: self.__name = newName else: print("error:名字长度需要大于或者等于5") xiaoming = People("Bj") print(xiaoming.__name)
print(xiaoming.__name) AttributeError: 'People' object has no attribute '__name'
class People(object): def __init__(self, name): self.__name = name def getName(self): return self.__name def setName(self, newName): if len(newName) >= 5: self.__name = newName else: print("error:名字长度需要大于或者等于5") xiaoming = People("Solar") xiaoming.setName("wanger") print(xiaoming.getName()) xiaoming.setName("lisi") print(xiaoming.getName())
对象名 = 类名()
# 定义类 class Car: # 移动 def move(self): print('车在奔跑...') # 鸣笛 def toot(self): print("车在鸣笛...嘟嘟..") # 创建一个对象,并用变量BMW来保存它的引用 BMW = Car() BMW.color = '黑色' BMW.wheelNum = 4 #轮子数量 BMW.move() BMW.toot() print(BMW.color) print(BMW.wheelNum)
def 类名: #初始化函数,用来完成一些默认的设定 def __init__(): pass
# 定义汽车类 class Car: def __init__(self): self.wheelNum = 4 self.color = '蓝色' def move(self): print('车在跑,目标:夏威夷') # 创建对象 BMW = Car() print('车的颜色为:%s'%BMW.color) print('车轮胎数量为:%d'%BMW.wheelNum)
import time class Animal(object): # 初始化方法 # 创建完对象后会自动被调用 def __init__(self, name): print('__init__方法被调用') self.__name = name # 析构方法 # 当对象被删除时,会自动被调用 def __del__(self): print("__del__方法被调用") print("%s对象马上被干掉了..."%self.__name) # 创建对象 dog = Animal("哈皮狗") # 删除对象 del dog cat = Animal("波斯猫") cat2 = cat cat3 = cat print("---马上 删除cat对象") del cat print("---马上 删除cat2对象") del cat2 print("---马上 删除cat3对象") del cat3 print("程序2秒钟后结束") time.sleep(2)
# 定义一个父类,如下: class Cat(object): def __init__(self, name, color="白色"): self.name = name self.color = color def run(self): print("%s--在跑"%self.name) # 定义一个子类,继承Cat类如下: class Bosi(Cat): def setNewName(self, newName): self.name = newName def eat(self): print("%s--在吃"%self.name) bs = Bosi("印度猫") print('bs的名字为:%s'%bs.name) print('bs的颜色为:%s'%bs.color) bs.eat() bs.setNewName('波斯') bs.run()
class Animal(object): def __init__(self, name='动物', color='白色'): self.__name = name self.color = color def __test(self): print(self.__name) print(self.color) def test(self): print(self.__name) print(self.color) class Dog(Animal): def dogTest1(self): #print(self.__name) #不能访问到父类的私有属性 print(self.color) def dogTest2(self): #self.__test() #不能访问父类中的私有方法 self.test() A = Animal() #print(A.__name) #程序出现异常,不能访问私有属性 print(A.color) #A.__test() #程序出现异常,不能访问私有方法 A.test() print("------分割线-----") D = Dog(name = "小花狗", color = "黄色") D.dogTest1() D.dogTest2()