首页 > 编程语言 >python7

python7

时间:2022-10-23 16:26:06浏览次数:39  
标签:__ show color self print python7 def

一、创建大雁类并定义飞行方法

class Geese:
'''大雁类'''
def __init__(self,beak,wing,claw):
print("我是大雁类!我有以下特征:")
print(beak)
print(wing)
print(claw)
def fly(self,state):
print(state)
beak_1="啄的基部较高,长度和头部的长度几乎相等"
wing_1="翅膀长而尖"
claw_1="爪子是蜉状的"
wildGoose=Geese(beak_1,wing_1,claw_1)
wildGoose.fly("我飞行的时候,一会儿拍成个人字,一会拍成个一字")

 

 二、通过类属性统计类的实例个数

class Geese:
'''雁类'''
neck="脖子较长"
wing="振翅频率高"
leg="腿位于身体的中心支架"
number=0
def __init__(self):
Geese.number+=1
print("\n我是第"+str(Geese.number)+"只大雁,我属于雁类!我有以下特征:")
print(Geese.neck)
print(Geese.wing)
print(Geese.leg)
list1=[]
for i in range(4):
list1.append(Geese())
print("一共有"+str(Geese.number)+"只大雁")

 

 三、在模拟电影点播功能时应用属性

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

 

 四、创建水果基类及其派生类

class Fruit:
color="绿色"
def harvest(self,color):
print("水果是:"+color+"的!")
print("水果已经收获……")
print("水果原来是:"+Fruit.color+"的!")
class Apple(Fruit):
color="红色"
def __init__(self):
print("我是苹果")
class Orange(Fruit):
color="橙色"
def __init__(self):
print("\n我是橘子")
apple=Apple()
apple.harvest(apple.color)
orange=Orange()
orange.harvest(apple.color)
orange.harvest(orange.color)

 

 五、在派生类中调用基类的__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("金黄色带紫色条纹")

 

 实战1、修改手机默认语言

class a:
def __init__(self):
print("智能手机的默认语言为英文")
def b(self,c):
print("将智能手机的默认语言设置为"+str(c))
m=a()
n=m.b("中文")

 

标签:__,show,color,self,print,python7,def
From: https://www.cnblogs.com/666k/p/16818766.html

相关文章

  • Python7-eg
    实例01(创建大雁类并定义飞行方法)1classGeese:2'''大雁类'''3def__init__(self,beak,wing,claw):4print("我是大雁类!我有以下特征:")5......