|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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)))
2个成人 + 1个小孩平日票价为:250.00
问题:
为什么 def __init__(self, weekend=False, child=False) 这里的weekend和child不需要self
还有 能不能 直接写成 def __init__(self, self.XXX, self.YYY) 后面就不需要再self化
因为这是需要在类外部传入的变量
另外,在类里面,不需要将self.的变量传递到内部函数中,因为可以直接用
|
|