@author jackzhenguo
@desc
@tag
@version
@date 2020/03/09
问:子类继承父类的方法吗?
答:子类的实例继承了父类的static_method静态方法,调用该方法,还是调用的父类的方法和类属性。
# coding:utf-8
class Foo(object):
X = 1
Y = 2
@staticmethod
def averag(*mixes):
return sum(mixes) / len(mixes)
@staticmethod
def static_method():
return Foo.averag(Foo.X, Foo.Y)
@classmethod
def class_method(cls):
return cls.averag(cls.X, cls.Y)
class Son(Foo):
X = 3
Y = 5
@staticmethod
def averag(*mixes):
return sum(mixes) / 3
p = Son()
print(p.static_method())
print(p.class_method())
# 1.5
# 2.6666666666666665