小编use*_*123的帖子

Python - typechecking确定何时不会抛出错误?

我有一些Python类,如果简化,看起来像:

class base:
    def __init__(self, v):
        self.value = v

    def doThings(self):
        print "Doing things."

    def doMoreThings(self):
        print "Doing more things."

    def combine(self, b):
        self.value += b.value

class foo(base):
    def showValue(self):
        print "foo value is %d." % self.value

class bar(base):
    def showValue(self):
        print "bar value is %d." % self.value
Run Code Online (Sandbox Code Playgroud)

base类包含的方法(通过上文表示doThingsdoMoreThings它们实现两者共同的功能性)foobar亚类.在foobar子类中,他们如何解释本质上的区别value领域.(上面,它们只是在打印它们时显示的不同,但在我的实际应用中,它们做了其他一些更复杂的事情.) base可以被认为是"抽象的":用户只能使用foos和bars. base仅存在于其子类共有的代码的主页.

我想问的方法是combine,它允许你取两个这样的对象并制作第三个.因为foobar解释 …

python oop

7
推荐指数
1
解决办法
96
查看次数

标签 统计

oop ×1

python ×1