关于python和类的问题

时间:2011-04-19 13:14:50

标签: python class function

我有一个基类,有一些用Python派生:

class Base:
   def Foo(self):
      pass

# First derived class
class Der1(Base):
   def OwnFoo(self):
      # Do something 1
   def OwnFoo2(self):
      # Do something 2

   def Foo(self):
      # Do something 3

# Second derived class
class Der2(Base):
   def OwnFoo(self):
      # Do something 1
   def OwnFoo2(self):
      # Do something 2

   def Foo(self):
      # Do something 3

问题是:

我在Der1中有一些预定义的代码。几乎Der2的所有函数都是这样做的。如何使用更少的代码编写它?

我无法将该代码添加到父级。不应该触及家长班。

例如,Der2.OwnFooDer1.OwnFoo的作用相同,也许python中有一些构造只是为了从第一类调用OwnFoo而不是再次编写该代码?


我无法更改Der1Der2的父级!它应该是Base

5 个答案:

答案 0 :(得分:3)

由于您无法更改继承结构,因此请创建一个包含公共代码的帮助程序类,并将其包含在composition而不是继承中。

# Common code goes in this new class
class DerHelper:
    def __init__(self, parent):
        self._parent = parent
    def OwnFoo(self):
        print 'Do something 1', self._parent
    def OwnFoo2(self):
        print 'Do something 2', self._parent
    def Foo(self):
        print 'Do something 3', self._parent

# First derived class
class Der1(Base):
    def __init__(self):
        # include helper class by composition
        self._helper = DerHelper('Der1')
    def OwnFoo(self):
        self._helper.OwnFoo()
    def OwnFoo2(self):
        self._helper.OwnFoo2()
    def Foo(self):
        self._helper.Foo()

# Second derived class
class Der2(Base):
    def __init__(self):
        # include helper class by composition
        self._helper = DerHelper('Der2')
    def OwnFoo(self):
        self._helper.OwnFoo()
    def OwnFoo2(self):
        self._helper.OwnFoo2()
    def Foo(self):
        self._helper.Foo()

当然,您可以将引用传递给父代,而不是字符串。我只是出于演示目的这样做。

用法:

d = Der1()
d.OwnFoo()
d.OwnFoo2()
d.Foo()

d = Der2()
d.OwnFoo()
d.OwnFoo2()
d.Foo()

输出:

Do something 1 Der1
Do something 2 Der1
Do something 3 Der1
Do something 1 Der2
Do something 2 Der2
Do something 3 Der2

答案 1 :(得分:2)

Der2成为Der1的子类,然后就完成了。

class Base:
   def Foo(self):
      pass

# First derived class
class Der1(Base):
   def OwnFoo(self):
      # Do something 1
   def OwnFoo2(self):
      # Do something 2

   def Foo(self):
      # Do something 3

# Second derived class (subclasses Der1)
class Der2(Der1):
   pass

您想要专门化的Der2内的任何行为都可以添加到类定义中。如果您在Der2中创建了同名的新方法(例如Der2.OwnFoo()),那么它会重载从Der1继承的默认方法。

编辑:如果您无法更改父级,请将要继承的所有行为放在基类中,请记住您可以重载或自定义子类中的任何方法。

在代码中:

# Base class
class Base:
    def Foo1(self):
        # Do something 1
    def Foo2(self):
        # Do something 2
    def Foo(self):
        # Do something 3

# First derived class, inherits everything from Base
class Der1(Base):
    pass

# Second derived class
class Der2(Base):
    pass  

您可以执行“技巧”来调用从父级继承的原始方法,捕获返回值,然后自定义行为。这只有在方法实际返回一个值时才有效,如果该方法操纵类中的属性可能会很危险,除非这是你想要的和期望的。

在代码中:

# Second derived class, with customized methods
class Der2(Base):
    # Anything that is not explicitly define is inherited from parent
    # as-is.

    def Foo(self):
        # My Foo() overloads Base.Foo() inherited from parent class. 
        # Do something "special" 3

    def Foo1(self):
        # Calls parent Base.Foo1() and then manipulates return data.
        base_output = Base.Foo1(self)
        # Do something "special" 1 with 'base_output'

答案 2 :(得分:1)

这是作业吗?

查看Der2的第一行:

class Der2(Base):

什么是它的父级(例如它下降的类和herits方法和属性)?你怎么能改变这个?

答案 3 :(得分:1)

如果Der1Der2分享了很多代码,那么你应该把它放在一个超类中;由于无法触及Base,因此请在两者之间引入一个类:

class Der(Base):
    def OwnFoo(self):
        ...

class Der1(Der):
    ...

class Der2(Der):
    ...

(根据您的班级层次结构,其他人推荐的“Der2来自Der1”选项也可能有效。)

答案 4 :(得分:0)

如何制作Der2子类Der1