1.创建大雁类并定义飞行方法
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("我飞行的时候,一会儿排成个人字,一会儿排成个一字")
结果:
2.通过类属性统计类的实例个数
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)
list = []
for i in range(4):
list.append(Geese())
print("一共有"+str(Geese.number)+"只大雁")
结果:
3.在模拟电影点播功能时应用属性
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)
结果:
4.创建水果基类及派生类
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(orange.color)
结果:
5.在派生类中调用基类的__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 Cellphone:
def __init__(self):
print("智能手机的默认语言为英文")
def langage(self,a):
print("将智能手机的默认语言设置为"+a)
Cellphone().langage("中文")
结果:
实战2:给信用卡设置默认密码
class Credit:
def __init__(self,cardnumber,password = "123456"):
if password == "123456":
print("信用卡"+cardnumber+"的默认密码为123456")
else:
print("重置信用卡"+cardnumber+"的密码为"+password)
Credit("4013735633800642")
Credit("4013735633800642","168779")
结果:
实战3.打印每月销售明细
class Sale:
commodity = (('T0001','笔记本电脑'),('T0002',"华为荣耀6X"),('T0003','iPad'),('T0004','华为荣耀V9'),('T0005','MacBock'))
def __init__(self,month):
if month == "2":
print("2月份的商品销售明细如下:")
for i in range(0,4):
print("商品编号:",Sale.commodity[i][0],"商品名称"+Sale.commodity[i][1])
month = (input("请输入要查询的月份(比如1,2,3,等):"))
sale = Sale(month)
else:
print("\n该月份没有销售数据或者输入有误!\n")
month = (input("请输入要查询的月份(比如1,2,3,等):"))
sale = Sale(month)
print("----------销售明细----------")
month = (input("请输入要查询的月份(比如1,2,3,等):"))
sale = Sale(month)
结果:
实战4:模拟电影院的自动售票机选票页面
class Ticketing_machine:
Films_name=''
seat=''
Movie_field=''
def __init__(self):
print('\n欢迎使用自动售票机~~')
pass
def Films(self,Films_name):
Ticketing_machine.Films_name = Films_name
print('已选电影:'+Films_name)
pass
def Movie_fields(self,Movie_field):
Ticketing_machine.Movie_field="2018.4.12 "+Movie_field
print('电影场次:' + Movie_field)
def seats(self,seat):
Ticketing_machine.seat = seat
print('选择座位:' + seat)
pass
def Cinema_ticket(self):
print("电影:"+Ticketing_machine.Films_name)
print("播出时间:" + Ticketing_machine.Movie_field)
print("座位:" + Ticketing_machine.seat)
pass
ticketing = Ticketing_machine()
print('\n请选择正在上映的电影:1、《长津湖》 2、《战狼》 3、《红海行动》')
ticketing.Films('《长津湖》')
print('\n请选择电影播放场次:1、9:30 2、10:40 3、12:00')
ticketing.Movie_fields('10:40')
print('\n请选择座位剩余座位:10-01,10-02,10-03,10-04')
ticketing.seats('10-3')
print('\n正在出票。。。\n')
ticketing.Cinema_ticket()
print("\n出票完成,请别忘记取票")
结果:
标签:__,color,self,面向对象,init,print,程序设计,def From: https://www.cnblogs.com/xiaoyouya/p/16830967.html