Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 841 Bytes

171.md

File metadata and controls

50 lines (36 loc) · 841 Bytes
@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
[上一个例子](170.md) [下一个例子](172.md)