类与对象
- 怎么定义私有方法?
在方式名称前加2个下斜杠 __
2.按照以下要求定义一个游乐园门票的类,并尝试计算2个成人+1个小孩平日票价。 要求:
平日票价100元
周末票价为平日的120%
儿童票半价
class Ticket():
def __init__(self,weekend=False,child=False):
self.exp = 100
if weekend:
self.inc = 1.2
else:
self.inc = 1
if child:
self.discount = 0.5
else:
self.discount = 1
def calcPrice(self,num):
return self.exp * self.inc * self.discount * num
adult = Ticket()
child = Ticket(child=True)
print('2个成人 + 1个小孩平日票价为:%.2f' %(adult.calcPrice(2)+child.calcPrice(1)))