实例01(创建大雁类并定义飞行方法)

1
class Geese: 2 '''大雁类''' 3 def __init__(self,beak,wing,claw): 4 print("我是大雁类!我有以下特征:") 5 print(beak) 6 print(wing) 7 print(claw) 8 def fly(self,state): 9 print(state) 10 '''********************调用方法**********************''' 11 beak_1 = "喙的基部较高,长度和头部长度几乎相等" 12 wing_1 = "翅膀长而尖" 13 claw_1 = "爪子是蹼状" 14 wlidGoose = Geese(beak_1,wing_1,claw_1) 15 wildGoose.fly("我飞行的时候,一会儿排成个人字,一会排成个一字") 16

 

 

实例02(通过类属性统计类的实例个数)

1
class Geese: 2 '''雁类''' 3 neck = "脖子很长" 4 wing = "振翅频率高" 5 leg = "腿位于身体的中心支点,行走自如" 6 number = 0 7 def __init__(self): 8 Geese.number +=1 9 print("\n我是第"+str(Geese.number)+"只大雁,我属于雁类!我有以下特征:") 10 print(Geese.neck) 11 print(Geese.wing) 12 print(Geese.leg) 13 #创建4个雁类的对象(相当于有4只大雁) 14 list1 = [] 15 for i in range(4): 16 list1.append(Geese()) 17 print("一共有"+str(Geese.number)+"只大雁")

 

 

实例03(在模拟电影点播功能时应用属性)

1
class TVshow: 2 list_film = ["战狼2","红海行动","西游记女儿国","熊出没.变形记"] 3 def __init__(self,show): 4 self.show = show 5 @property 6 def show(self): 7 return self.__show 8 @show.setter 9 def show(self,value): 10 if value in TVshow.list_film: 11 self.__show = "您选择了《" + value +"》,稍后将播放" 12 else: 13 self.__show = "您点播的电影不存在" 14 tvshow = TVshow("战狼2") 15 print("正在播放:《",tvshow.show,"") 16 print("您可以从",tvshow.list_film,"中选择要点播放的电影") 17 tvshow.show = "红海行动" 18 print(tvshow.show)

 

 

实例04(创建水果基类及其派生类)

1
class Fruit: 2 color = "绿色" 3 def harvest(self,color): 4 print("水果是:" +color + "的!") 5 print("水果已经收获......") 6 print("水果原来是:" + Fruit.color + "的!"); 7 class Apple(Fruit): 8 color = "红色" 9 def __init__(self): 10 print("我是苹果") 11 class Orange(Fruit): 12 color = "橙色" 13 def __init__(self): 14 print("\n我是橘子") 15 apple = Apple() 16 apple.harvest(apple.color) 17 orange = Orange() 18 orange.harvest(orange.color)

 

 

实例05(在派生类中调用基类的__init__()方法定义类属性)

class
Fruit: def __init__(self,color = "绿色"): Fruit.color = color def harvest(self,color): print("水果是:" +self.color + "的!") print("水果已经收获......") print("水果原来是:" + Fruit.color + "的!"); class Apple(Fruit): color = "红色" def __init__(self): print("我是苹果") super().__init__() class Sapodilla(Fruit): def __init__(self,color): print("\n我是人参果") super().__init__(color) def harvest(self,color): print("人参果是:"+ color +"的!") print("人参果已经收获......") print("人参果原来是:" + Fruit.color + "的!") apple = Apple() apple.harvest(apple.color) sapodilla = Sapodilla("白色") sapodilla.harvest("金黄色带紫色条纹")

 

原文地址:http://www.cnblogs.com/ron312/p/16810789.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性