关于子类化的问题 - 版本2 [英] Question about subclassing - version 2

查看:52
本文介绍了关于子类化的问题 - 版本2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我最近发布了一个关于子类化的问题。我没有非常清楚地解释我的

全部要求,我建议的解决方案并不漂亮。

我会尝试解释我想要做的更充分,

描述了一种可能的解决方案。它仍然不漂亮,所以我会赞赏任何评论。


我有一个基类(ClassA),这是一个抽象类。大多数

方法和属性对于所有子类都是通用的,因此他们不得不重写




我有一个基类的子类(ClassB),它也是抽象的。它

代表ClassA的一个子集,并覆盖它的一些方法。当

我创建一个具体的类(是正确的术语吗?)我从ClassA或ClassB继承了




现在我想代表ClassA的一个不同子集,它会覆盖它的一些方法。这个子集可以应用于ClassB以及

ClassA。


在伪术语中,我想要ClassA1,ClassA2,ClassB1和ClassB2,其中A1

是基类,B覆盖了一些方法,2覆盖了其他

方法,我想从其中任何一个子类化。


我的原始解决方案涉及传递​​1或2作为参数,并且

将一些代码放入__init__,如果它重新定义了某些方法

收到了2.这有效,但是这意味着我不能轻易地在一个具体的类中重新定义
方法。


我的新想法是使用多重继承。这就是它的工作原理。


class ClassA(对象):

def __init __(自我):

传递

def test1(个体经营):

打印''基本方法1''

def test2(个体经营):

打印''基本方法2''


类ClassB(ClassA):

def __init __(自我):

ClassA。 __init __(self)

def test1(self):

print''覆盖方法1''

class Class2(object ):

def test2(个体经营):

打印''覆盖方法2''


现在我可以设置以下具体课程:


class ClassA1(ClassA):

def __init __(self):

ClassA .__ init __(self)


class ClassA2(Class2,ClassA):

def __init __(self):

ClassA .__ init __(self)


class ClassB1(ClassB):

def __init __(self):

ClassB .__ init __(self)


class ClassB2(Class2,ClassB):

def __init __(自我):

ClassB .__ init __(self)


现在,如果我执行以下操作,我会得到显示的结果,这是什么我想要
想要 -


ClassA1()。test1() - ''基本方法1''

ClassA1()。 test2() - ''基本方法2''

ClassB1()。test1() - ''覆盖方法1''

ClassB1()。test2() - ''基本方法2''

ClassA2()。test1() - ''基本方法1''

ClassA2()。test2() - ''覆盖方法2''

ClassB2()。test1() - ''覆盖方法1''

ClassB2()。test2() - ''覆盖方法2'' br />

现在进行真正的测试 -


class ClassC3(Class2,ClassB):

def __init __(self) :

ClassB .__ init __(self)

def test1(self):

print''从ClassC3覆盖方法1''br / >
def test2(个体经营):

print''从ClassC3覆盖方法2''


C lassC3()。test1() - ''从ClassC3覆盖方法1''

ClassC3()。test2() - ''从ClassC3覆盖方法2'


所以它有效。但是,使用多重继承并不理想,而且我认为某些语言甚至不支持它。任何人都可以建议

a解决这个问题的更好方法吗?


谢谢


Frank Millman
< br>

Hi all

I recently posted a question about subclassing. I did not explain my
full requirement very clearly, and my proposed solution was not pretty.
I will attempt to explain what I am trying to do more fully, and
describe a possible solution. It is still not pretty, so I would
appreciate any comments.

I have a base class (ClassA), which is an abstract class. Most of the
methods and attributes are common to all subclasses, so there is not
much they have to override.

I have a subclass (ClassB) of my base class, which is also abstract. It
represents a subset of ClassA, and overrides some of its methods. When
I create a concrete class (is that the correct term?) I subclass either
from ClassA or from ClassB.

Now I want to represent a different subset of ClassA, which overrides
some of its methods. This subset can apply to ClassB as well as to
ClassA.

In pseudo terms, I want ClassA1, ClassA2, ClassB1, and ClassB2 where A1
is the base class, B overides some methods, and 2 overrides other
methods, and I want to subclass from any of them.

My original solution involved passing 1 or 2 as an argument, and
putting some code into __init__ which redefined certain methods if it
received a 2. This worked, but it meant that I could not then easily
redefine the method again in a concrete class.

My new idea is to use multiple inheritance. This is how it would work.

class ClassA(object):
def __init__(self):
pass
def test1(self):
print ''Base method 1''
def test2(self):
print ''Base method 2''

class ClassB(ClassA):
def __init__(self):
ClassA.__init__(self)
def test1(self):
print ''Overriding method 1''

class Class2(object):
def test2(self):
print ''Overriding method 2''

Now I can set up the following concrete classes -

class ClassA1(ClassA):
def __init__(self):
ClassA.__init__(self)

class ClassA2(Class2,ClassA):
def __init__(self):
ClassA.__init__(self)

class ClassB1(ClassB):
def __init__(self):
ClassB.__init__(self)

class ClassB2(Class2,ClassB):
def __init__(self):
ClassB.__init__(self)

Now if I do the following, I get the results shown, which is what I
want -

ClassA1().test1() - ''Base method 1''
ClassA1().test2() - ''Base method 2''
ClassB1().test1() - ''Overriding method 1''
ClassB1().test2() - ''Base method 2''
ClassA2().test1() - ''Base method 1''
ClassA2().test2() - ''Overriding method 2''
ClassB2().test1() - ''Overriding method 1''
ClassB2().test2() - ''Overriding method 2''

Now for the real test -

class ClassC3(Class2,ClassB):
def __init__(self):
ClassB.__init__(self)
def test1(self):
print ''Overriding method 1 from ClassC3''
def test2(self):
print ''Overriding method 2 from ClassC3''

ClassC3().test1() - ''Overriding method 1 from ClassC3''
ClassC3().test2() - ''Overriding method 2 from ClassC3''

So it works. However, using multiple inheritance is not ideal, and I
believe it is not even supported in some languages. Can anyone suggest
a better way of tackling this problem?

Thanks

Frank Millman

推荐答案

Frank Millman,只是一个简短的说明,更专业的人可以给你

更好的答案。 Python中没有抽象类。他们都是混凝土。您可能有未定义方法的类(它们可能会引发

NotImplementedError)。

Java和Ruby不支持多重继承,但它是

支持C ++和Python,所以你可以在Python中使用它。

还有混合方法的方法。您可以定义方法,然后

添加到您的班级的方法列表。


再见,

bearophile
Frank Millman, just a short note, more expert people can give you
better answers. There aren''t abstract classes in Python. They are all
concrete. You may have classes with undefined methods (they may raise
NotImplementedError).
Multiple inheritance isn''t supported by Java and Ruby, but it is
supported by C++ and Python, so you can use it in Python.
There are also ways of mixing methods. You may define methods, and then
lists of methods to add to your classes.

Bye,
bearophile



是*** *********@lycos.com 写道:

弗兰克米尔曼,只是一个简短的说明,更专业的人可以给你

更好的答案。 Python中没有抽象类。他们都是混凝土。您可能有未定义方法的类(它们可能会引发

NotImplementedError)。

Java和Ruby不支持多重继承,但它是

支持C ++和Python,所以你可以在Python中使用它。

还有混合方法的方法。你可以定义方法,然后

添加到你班级的方法列表。


再见,

bearophile
Frank Millman, just a short note, more expert people can give you
better answers. There aren''t abstract classes in Python. They are all
concrete. You may have classes with undefined methods (they may raise
NotImplementedError).
Multiple inheritance isn''t supported by Java and Ruby, but it is
supported by C++ and Python, so you can use it in Python.
There are also ways of mixing methods. You may define methods, and then
lists of methods to add to your classes.

Bye,
bearophile



我在抽象意义上使用术语抽象类:-)


假设我有三个班级,其中90%属性和方法是常见的
。创建一个具有这些属性和

方法的基类是有意义的,并将这三个类中的每一个转换为一个子类,

继承自基类并覆盖位每一个都是独一无二的。


这就是我所谓的抽象类。也许有一个更正确的

术语。


Frank

I use the term ''abstract class'' in the abstract sense :-)

Say I have three classes where 90% of the attributes and methods are
common. It makes sense to create a base class with these attributes and
methods, and turn each of the three classes into a subclass which
inherits from the base class and overrides the bits that are unique to
each one.

This is what I call an abstract class. Maybe there is a more correct
term.

Frank


Frank Millman写道:
Frank Millman wrote:

大家好


我最近发布了一个关于子类化的问题。我没有非常清楚地解释我的

全部要求,我建议的解决方案并不漂亮。

我会尝试解释我想要做的更充分,

描述了一种可能的解决方案。它仍然不漂亮,所以我会赞赏任何评论。


我有一个基类(ClassA),这是一个抽象类。大多数

方法和属性对于所有子类都是通用的,因此他们不得不重写




我有一个基类的子类(ClassB),它也是抽象的。它

代表ClassA的一个子集,并覆盖它的一些方法。当

我创建一个具体的类(是正确的术语吗?)我从ClassA或ClassB继承了




现在我想代表ClassA的一个不同子集,它会覆盖它的一些方法。这个子集可以应用于ClassB以及

ClassA。


在伪术语中,我想要ClassA1,ClassA2,ClassB1和ClassB2,其中A1

是基类,B覆盖了一些方法,2覆盖了其他

方法,我想从其中任何一个子类化。


我的原始解决方案涉及传递​​1或2作为参数,并且

将一些代码放入__init__,如果它重新定义了某些方法

收到了2.这有效,但是这意味着我不能轻易地在一个具体的类中重新定义
方法。


我的新想法是使用多重继承。这就是它的工作原理。


class ClassA(对象):

def __init __(自我):

传递

def test1(个体经营):

打印''基本方法1''

def test2(个体经营):

打印''基本方法2''


类ClassB(ClassA):

def __init __(自我):

ClassA。 __init __(self)

def test1(self):

print''覆盖方法1''

class Class2(object ):

def test2(个体经营):

打印''覆盖方法2''
Hi all

I recently posted a question about subclassing. I did not explain my
full requirement very clearly, and my proposed solution was not pretty.
I will attempt to explain what I am trying to do more fully, and
describe a possible solution. It is still not pretty, so I would
appreciate any comments.

I have a base class (ClassA), which is an abstract class. Most of the
methods and attributes are common to all subclasses, so there is not
much they have to override.

I have a subclass (ClassB) of my base class, which is also abstract. It
represents a subset of ClassA, and overrides some of its methods. When
I create a concrete class (is that the correct term?) I subclass either
from ClassA or from ClassB.

Now I want to represent a different subset of ClassA, which overrides
some of its methods. This subset can apply to ClassB as well as to
ClassA.

In pseudo terms, I want ClassA1, ClassA2, ClassB1, and ClassB2 where A1
is the base class, B overides some methods, and 2 overrides other
methods, and I want to subclass from any of them.

My original solution involved passing 1 or 2 as an argument, and
putting some code into __init__ which redefined certain methods if it
received a 2. This worked, but it meant that I could not then easily
redefine the method again in a concrete class.

My new idea is to use multiple inheritance. This is how it would work.

class ClassA(object):
def __init__(self):
pass
def test1(self):
print ''Base method 1''
def test2(self):
print ''Base method 2''

class ClassB(ClassA):
def __init__(self):
ClassA.__init__(self)
def test1(self):
print ''Overriding method 1''

class Class2(object):
def test2(self):
print ''Overriding method 2''



待迂腐,Class2.test2没有覆盖任何东西,因为有

没有test2在它的父类中的方法。

To be pedantic, Class2.test2 is not overridding anything, since there''s
no "test2" method in it''s parent class.


现在我可以设置以下具体类 -


class ClassA1( ClassA):

def __init __(自我):

ClassA .__ init __(self)
Now I can set up the following concrete classes -

class ClassA1(ClassA):
def __init__(self):
ClassA.__init__(self)



如果那样'这是你在__init__中唯一做的事情,然后根本不打算写一个初始化方法。

If that''s the only thing you do in the __init__, then don''t bother write
an init method at all.


class ClassA2 (Class2,ClassA):

def __init __(self):

ClassA .__ init __(self)
class ClassA2(Class2,ClassA):
def __init__(self):
ClassA.__init__(self)



我可以建议看一下super()吗?

May I suggest having a look at super() ?


class ClassB1(ClassB):

def __init __(self):

ClassB .__ init __(self)


class ClassB2(Class2,ClassB):

def __init __(self):

ClassB .__ init __(self)


现在,如果我执行以下操作,我会看到显示的结果,这就是我想要的结果 -


ClassA1()。test1() - ''基本方法1''

ClassA1()。test2() - ''基本方法2''
ClassB1()。test1() - ''覆盖方法1''

ClassB1()。test2() - ''基本方法2''

ClassA2()。test1() - ''基本方法1''

ClassA2()。test2() - ''覆盖方法2''

ClassB2() .test1() - ''覆盖方法1''

ClassB2()。test2() - ''覆盖方法2''


现在为真正的测试 -


class ClassC3(Class2,ClassB):

def __init __(self):

ClassB .__ init __(self )

def test1(self):

print''从ClassC3覆盖方法1''

def test2(self):

print''从ClassC3覆盖方法2''


ClassC3()。test1() - ''从ClassC3覆盖方法1''

ClassC3()。test2() - ''从ClassC3覆盖方法2 ''


所以它有效。但是,使用多重继承并不理想,
class ClassB1(ClassB):
def __init__(self):
ClassB.__init__(self)

class ClassB2(Class2,ClassB):
def __init__(self):
ClassB.__init__(self)

Now if I do the following, I get the results shown, which is what I
want -

ClassA1().test1() - ''Base method 1''
ClassA1().test2() - ''Base method 2''
ClassB1().test1() - ''Overriding method 1''
ClassB1().test2() - ''Base method 2''
ClassA2().test1() - ''Base method 1''
ClassA2().test2() - ''Overriding method 2''
ClassB2().test1() - ''Overriding method 1''
ClassB2().test2() - ''Overriding method 2''

Now for the real test -

class ClassC3(Class2,ClassB):
def __init__(self):
ClassB.__init__(self)
def test1(self):
print ''Overriding method 1 from ClassC3''
def test2(self):
print ''Overriding method 2 from ClassC3''

ClassC3().test1() - ''Overriding method 1 from ClassC3''
ClassC3().test2() - ''Overriding method 2 from ClassC3''

So it works. However, using multiple inheritance is not ideal,



为什么会这样?多重继承是一个非常有用的工具 - 但它很快就会变得棘手。恕我直言,它最好用于mixin课程......

Why so ? Multiple inheritence is a pretty useful tool - but it can
become tricky very soon. IMHO, it''s best use is for mixin classes...


和我

相信它甚至不支持某些语言。
and I
believe it is not even supported in some languages.



在某些语言中甚至不支持很多东西! - )

A lot of things aren''t even supported in some languages !-)


任何人都可以建议

a更好的解决这个问题的方法?
Can anyone suggest
a better way of tackling this problem?



不是我的帽子。关于Python和OO的一些注意事项:Python

是动态类型的,继承只是关于共享

的实现。还有另一种分享实施方式 -

组成/授权。它更灵活,可以避免笛卡儿式的产品。类的乘法。它的宣传也不如

继承 - 可能是因为某些语言而已。没有提供

任何支持。这里的好消息是,由于__getattr __ / __ setattr__钩子,Python让它变得轻而易举,

。现在我不知道是不是它b / b
有任何意义WRT /你当前的问题...

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')) ])

p in''o **** @ xiludom.gro''split(''@'')])"

Not out of my hat. Just a few considerations on Python and OO: Python
being dynamically typed, inheritence is only about sharing
implementation. There''s another way to do share implementation -
composition/delegation. It''s more flexible, and can avoid "cartesian
product" multiplication of classes. It''s also less advertised than
inheritance - probably because of "some languages" that fail to offer
any support for it. The good news here is that Python makes it a breeze,
thanks to the __getattr__/__setattr__ hooks. Now I don''t know if it
makes any sense WRT/ your current problem...
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于关于子类化的问题 - 版本2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆