继承但只是部分? [英] Inheritance but only partly?

查看:54
本文介绍了继承但只是部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个X级,它有10种方法。


我希望Y级继承其中的5种。


我能这样做吗?我可以做一些超级线路(Y,排除

方法3 4 7 9 10)?

Let''s say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?

推荐答案

process写道:
process wrote:

假设我有一个X类,它有10种方法。


我想要Y类继承其中的5个。


我可以这样做吗?我可以按照超级的方式做一些事情(Y,排除

方法3 4 7 9 10)?


-
http://mail.python.org/mailman/listinfo/python-list



No.


但为什么要关心?你可以忽略你不想要的5 - 他们的

存在你的内存或执行速度都没有。


你也可以重新定义那些你不想继承的:


A级:

def DontInheritMe(自我,......):

...


B类(A):

def DontInheritMe(self,...):

引发NotImplementedError //或者一些这样的

加里赫伦

No.

But why do yo care? You can just ignore the 5 you don''t want -- their
existence costs you nothing in either memory or execution speed.

You can also redefine the ones you don''t want inherited:

class A:
def DontInheritMe(self, ...):
...

Class B(A):
def DontInheritMe(self, ...):
raise NotImplementedError // Or some such
Gary Herron


10月2日,1:16 * pm,处理< circularf .. 。@ gmail.comwrote:
On Oct 2, 1:16*pm, process <circularf...@gmail.comwrote:

假设我有一个X类,它有10种方法。


我希望Y级继承其中的5个。


我可以这样做吗?我可以按照超级(Y,排除

方法3 4 7 9 10)做一些事情吗?
Let''s say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?



我认为这样做的原因是将原始类别

分成两个类。


你有什么:


类BaseClass(对象):


def方法0:

通过


def method1:

pass


def method2:

pass


...


def方法9:

通过


什么你需要:

类BaseClassWant(对象):

def方法0:

通过


def方法1:

通过


...


def method4:

通过


类BaseClassDontWant(对象):

def方法5:

通过


def方法6:

通过


...


def method9:

传递


类BaseClass(BaseClassWant,BaseClassDontWant):#与BaseClass相同

以上

传递

类YourC lass(BaseClassWant):

通过

Matt

I think the noral way of doing that is to split the origional class
into two classes.

What you have:

class BaseClass(object):

def method0:
pass

def method1:
pass

def method2:
pass

...

def method9:
pass

What you need:
class BaseClassWant(object):
def method0:
pass

def method1:
pass

...

def method4:
pass

class BaseClassDontWant(object):
def method5:
pass

def method6:
pass

...

def method9:
pass

class BaseClass(BaseClassWant, BaseClassDontWant): # same as BaseClass
above
pass

class YourClass(BaseClassWant):
pass
Matt


10月2日,3:16 * pm,处理< ; circularf ... @ gmail.comwrote:
On Oct 2, 3:16*pm, process <circularf...@gmail.comwrote:

假设我有一个X类,它有10种方法。


我希望Y级继承其中的5个。


我可以这样做吗?我可以按照超级(Y,排除

方法3 4 7 9 10)做一些事情吗?
Let''s say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?



这意味着你所做的5包括不要依赖或打电话给你5

不要。否则你必须继承它们。然后你可以重构

吧:


class X5YouDo:...

class X5YouDont:...

class X(X5YouDo,X5YouDont):...

class Y(X5YouDo):...


如果你是寻找有限的能见度,Python没有它。

它只是手铐,并且做了你不能做的事情。


毕竟什么都不能阻止你的用户打电话:


y = Y()

X5YouDont.DontInheritMe(y,args)


来获取未经授权的方法。

That implies that the 5 you do include don''t rely on or call the 5 you
don''t. Otherwise you have to inherit them. Then you can refactor
them into:

class X5YouDo: ...
class X5YouDont: ...
class X( X5YouDo, X5YouDont ): ...
class Y( X5YouDo ): ...

If you''re looking for restricted visibility, Python does not have it.
It''s just handcuffs, and makes things you can''t do.

After all, nothing would stop you user from calling:

y= Y()
X5YouDont.DontInheritMe( y, args )

to get at the uninherited methods.


这篇关于继承但只是部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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