一个类的多个扩展 [英] multiple extensions to a class

查看:72
本文介绍了一个类的多个扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用不同方法扩展一个类(用于GUI的

插件框架),其中有一些扩展名为

这些扩展需要合作,但其他人不可能知道每一个

其他。


即如果我有A级,我想要添加一个功能块,我可以将它导出到一个增加了这种功能的B中。如果我想添加更多

功能,我可以将B派生成C.但是如果我想将第三位

功能D直接添加到A,那么D对B或C一无所知,我没有能够实例化具有所有三个扩展名的对象,即

和ABCD。它可以是ABC,也可以是ABC。或者AD。


一种可能性是将扩展B,C和D定义为函数,并且

将这些函数添加到A中.instancemethod。关于如何使用
的其他任何想法都可以做到这一点?例如。是否可以*为类添加*基类?我可以通过将它们作为基类添加到A来添加

扩展。


谢谢,

Oliver
< br <>

解决方案

Humpty Dumpty写道:

你好,我正在尝试不同的扩展课程方式(一个<当一些这些扩展需要协作时,有一个以上扩展的GUI插件框架,但是其他人不可能知道每个扩展。
功能,我可以将B派生成C.但是如果我想直接向A添加第三位
功能D,那么D对B或C一无所知,我无法实例化具有所有三个扩展名的对象,即
一个ABCD。它可以是ABC,也可以是ABC。或者AD。

一种可能性是将扩展B,C和D定义为函数,并使用new.instancemethod将这些函数添加到A.关于如何做到这一点的任何其他想法?例如。是否可以*为类添加*基类?我可以通过添加它们作为基类来添加
扩展。

谢谢,
Oliver




我使用函数做模板类:


def B(基类):

class B(基类):

def f(self,x,y):

#do stuff

super(B,self).f(x,y)

返回B


def C(基础类):

C级(基础类):

def f(self,x ,y):

#做东西

super(C,self).f(x,y)

返回C


extendedA = C(B(A))

extendedA.f(3,4)


我想你能找到在Python菜谱中这样的食谱...


David


Humpty Dumpty写道:

一种可能性是将扩展B,C和D定义为函数,并使用new.instancemethod将这些函数添加到A中。关于如何做到这一点的任何其他想法?例如。是否可以*为类添加*基类?我可以通过将它们作为基类添加到A来添加扩展。




您可以考虑将A类放入某种容器中 - 即,

给它一些地方来存储装饰者列表。对象,然后将B $,B,C和D转换成这样的装饰器(而不是A的子类)。你要

必须安排一些方式将消息发送到A,这对于装饰者来说是打算的,并且可能是某些方式让装饰者与

彼此。 (很难说你是否会更好地制作B和

C单独的装饰者,或者是否从B继承C - 取决于

您的具体申请。)


Jeff Shannon

技术员/程序员

Credit International


Jeff Shannon写道:

Humpty Dumpty写道:

一种可能性是定义扩展B,C和D作为
函数和
使用new.instancemethod将这些函数添加到A.关于
如何做到这一点的任何其他想法?例如。是否可以*为类添加*基类?我可以通过将它们作为基类添加到A来添加
扩展。



您可以考虑将A类放入某种容器中 - 即,
给它一个地方来存储装饰者列表。对象,然后使B,C和D成为这样的装饰器(而不是A的子类)。你将不得不安排一些方式将消息发送给A,这些消息是为装饰者设计的,并且可能是装饰者彼此交谈的某种方式。 (很难说你是否会更好地制作B和
C单独的装饰器,或者是否从B继承C - 取决于你的具体应用。)



这里Python的优势在于你不需要从另一个继承子类中的一个 - 只需明确定义你期望的方法,并且

然后有一个系统,你可以在一个监听类列表上调用方法...


David


Hello, I''m experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn''t know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won''t be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.

Thanks,
Oliver

解决方案

Humpty Dumpty wrote:

Hello, I''m experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn''t know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won''t be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.

Thanks,
Oliver



I do a template class using a function:

def B(baseclass):
class B(baseclass):
def f(self, x, y):
# do stuff
super(B, self).f(x, y)
return B

def C(baseclass):
class C(baseclass):
def f(self, x, y):
# do stuff
super(C, self).f(x, y)
return C

extendedA = C(B(A))
extendedA.f(3,4)

I think you can find recipes like this in the Python cookbook...

David


Humpty Dumpty wrote:

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.



You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You''ll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other. (It''s hard to say whether you''d be better off making B and
C separate decorators, or whether to subclass C from B -- depends on
your specific application.)

Jeff Shannon
Technician/Programmer
Credit International


Jeff Shannon wrote:

Humpty Dumpty wrote:

One possibility would be to define the extensions B,C and D as
functions and
add those functions to A with new.instancemethod. Any other ideas on
how to
do this? E.g. is it possible to *add* base classes to a class? I could
add
the extensions by adding them as base classes to A.



You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You''ll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other. (It''s hard to say whether you''d be better off making B and
C separate decorators, or whether to subclass C from B -- depends on
your specific application.)


The advantage of Python here is that you don''t need to subclass one from
the other - just make a clear definition of the methods you expect, and
then have a system where you call methods on a list of listening classes...

David


这篇关于一个类的多个扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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