class Times(object):
factor = 1
# 定义一个类方法,用于修改类的默认参数
@classmethod
def mul(cls,x):
return cls.factor * x
class TwoTimes(Times):
factor = 2
# 调用Times.mul(TwoTimes,4)
x = TwoTimes.mul(4)
print(x)
运行结果
8
class Times(object):
factor = 1
# 定义一个类方法,用于修改类的默认参数
@classmethod
def mul(cls,x):
return cls.factor * x
class TwoTimes(Times):
factor = 2
# 调用Times.mul(TwoTimes,4)
x = TwoTimes.mul(4)
print(x)
运行结果
8