覆盖返回基类对象的方法 [英] overriding method that returns base class object
问题描述
我有一个来自第三方的A级,我无法更改
并且用C实现。我从A
派生我自己的B级并添加一对新方法并覆盖方法。
问题是A有一个方法(称之为A.f()),它创建了
并返回一个新的A对象。我需要B.f()来返回从A.f()派生的B
对象。让
发生的最佳方法是什么?
I have a class A from a third party that I cannot change
and is implemented in C. I derive my own class B from A
and add a couple new methods and override a method. The
problem is that A has a method (call it A.f() ) that creates
and returns a new A object. I need B.f() to return a B
object derived from A.f(). What is the best way to make
that happen?
推荐答案
Stuart McGraw写道:
Stuart McGraw wrote:
我有来自第三方的A类,我无法改变
并在C中实现。我从A
派生了我自己的B类并添加了几个新方法并覆盖一个方法。问题是A有一个方法(称之为A.f())创建
并返回一个新的A对象。我需要B.f()来返回从A.f()派生的B
对象。什么是使
发生的最佳方法?
I have a class A from a third party that I cannot change
and is implemented in C. I derive my own class B from A
and add a couple new methods and override a method. The
problem is that A has a method (call it A.f() ) that creates
and returns a new A object. I need B.f() to return a B
object derived from A.f(). What is the best way to make
that happen?
如果我理解正确,它与
$ b $无关事实上,父类是用C实现的,你只需要知道一些不常见的语法:
A类:
def f(个体经营):
返回A()
B级(A):
def f(self ):
obj = Af(自我)
#做你想做的任何事情
返回obj
关键是你所说的来自Af()的B对象。如果
派生你的意思与_inheritance_有关,那么
要么你不理解继承,要么你不清楚是什么
你想要的。
如果你只是想要B''sf()做一些特别的事情
Af()返回的对象,那么上面的代码应该让你
正确地做到这一点...
-Peter
If I understand this correctly, it has nothing to do with the
fact that the parent class is implemented in C and you just
need to know a little uncommon syntax:
class A:
def f(self):
return A()
class B(A):
def f(self):
obj = A.f(self)
# do whatever you want to obj here
return obj
The key is what you mean by "a B object derived from A.f()". If
by derived you mean something to do with _inheritance_, then
either you don''t understand inheritance or you weren''t clear what
you wanted.
If you just mean you want B''s f() to do something special to the
A object that A.f() returns, then the above code should let you
do that properly...
-Peter
对不起,你是对的,我不清楚。我的意思是B继承自
A.这是我想要做的......
A类有一个返回A的方法Aa()我想要一个
相同的类但是有一个额外的属性.newprop
和方法.b()我希望.a()返回B,而不是A 。
B级(A):
def __init __(自我,* args,** kwds):
A. __init __(self,* args,** kwds)
self.newprop = 99
def a(self):
x = Aa(self )#x是A
x .__ class__ = B
返回x#我希望x为B,即有b()和.newprop。
def b(个体经营):
......东西......
是的,我知道这是假的。但我不确定我应该做什么。并且为了纠正我最初发布的内容,A是在python中实现的(但我仍然不能因为管理原因而改变它的b $ b),但它的属性
声明为__ slots__ = [...]如果这产生了
的差异。这完全在Python 2.3.3中。
" Peter Hansen" < PE *** @ engcorp.com>在消息新闻中写道:40 *************** @ engcorp.com ...
Sorry, you are right, I wasn''t clear. I mean B inherits from
A. Here is what I am trying to do...
Class A has a method A.a() that returns an A. I want a
identical class but with an additional property .newprop
and method .b() And I want .a() to return a B, not an A.
class B (A):
def __init__(self, *args, **kwds):
A.__init__(self, *args, **kwds)
self.newprop = 99
def a(self):
x = A.a(self) # x is an A
x.__class__ = B
return x # I want x to be a B, i.e have b() and .newprop.
def b(self):
...something...
Yes, I know this is bogus. But I am not sure what
I should be doing. And to correct what I originally
posted, A is implented in python (but I still can''t
change it for administrative reasons), but it''s properties
are declared with "__slots__ = [...]" if that makes a
difference. This is all in Python 2.3.3.
"Peter Hansen" <pe***@engcorp.com> wrote in message news:40***************@engcorp.com...
Stuart McGraw写道:
Stuart McGraw wrote:
并在C中实现。我从A
派生我自己的B类并添加一些新方法并覆盖一个方法。问题是A有一个方法(称之为A.f())创建
并返回一个新的A对象。我需要B.f()来返回从A.f()派生的B
对象。使
发生的最佳方法是什么?
I have a class A from a third party that I cannot change
and is implemented in C. I derive my own class B from A
and add a couple new methods and override a method. The
problem is that A has a method (call it A.f() ) that creates
and returns a new A object. I need B.f() to return a B
object derived from A.f(). What is the best way to make
that happen?
如果我理解正确的话,它与实现父类的事实无关在C和你只是需要知道一些不常见的语法:
def f(个体经营):
返回A()
se B级(A):
def f(个体经营):
obj = Af(个体经营)
#做你想做的任何事情
返回obj
关键是你的意思是从Af()得到的B对象。如果
派生你的意思与_inheritance_有关,那么
要么你不理解继承,要么你不清楚你想要什么。
>如果你只是想要B''sf()为Af()返回的对象做一些特殊的事情,那么上面的代码应该让你这样做......
-Peter
If I understand this correctly, it has nothing to do with the
fact that the parent class is implemented in C and you just
need to know a little uncommon syntax:
class A:
def f(self):
return A()
se
class B(A):
def f(self):
obj = A.f(self)
# do whatever you want to obj here
return obj
The key is what you mean by "a B object derived from A.f()". If
by derived you mean something to do with _inheritance_, then
either you don''t understand inheritance or you weren''t clear what
you wanted.
If you just mean you want B''s f() to do something special to the
A object that A.f() returns, then the above code should let you
do that properly...
-Peter
文章< 40 ****************** ***@news.frii.net>,
Stuart McGraw< sm ****** @ frii.RemoveThisToReply.com>写道:
In article <40*********************@news.frii.net>,
Stuart McGraw <sm******@frii.RemoveThisToReply.com> wrote:
A类有一个返回A的方法Aa()。我想要一个相同的类,但有一个额外的属性.newprop
和方法。 b()我希望.a()返回B,而不是A.
B类(A):
def __init __(self,* args,** kwds):
A .__ init __(self,* args,** kwds)
self.newprop = 99
def a(self):
x = Aa(self)#x是一个A
x .__ class__ = B
返回x#我希望x为B,即有b()和.newprop。
def b(self):
..某事......
是的,我知道这是假的。但是我不确定我应该做什么。
为了纠正我最初发布的内容,A在python中实现了
(但由于行政原因我仍然无法改变它),但它'声明的属性是用__ slots__ = [...]声明的。如果这会产生差异。这完全在Python 2.3.3中。
Class A has a method A.a() that returns an A. I want a
identical class but with an additional property .newprop
and method .b() And I want .a() to return a B, not an A.
class B (A):
def __init__(self, *args, **kwds):
A.__init__(self, *args, **kwds)
self.newprop = 99
def a(self):
x = A.a(self) # x is an A
x.__class__ = B
return x # I want x to be a B, i.e have b() and .newprop.
def b(self):
...something...
Yes, I know this is bogus. But I am not sure what I should be doing.
And to correct what I originally posted, A is implented in python
(but I still can''t change it for administrative reasons), but it''s
properties are declared with "__slots__ = [...]" if that makes a
difference. This is all in Python 2.3.3.
A类:
def a(个体经营):
return self .__ class __()
-
Aahz(aa**@pythoncraft.com)< *> http://www.pythoncraft.com/
争辩你的局限,果然他们是你的。 - 理查德巴赫
class A:
def a(self):
return self.__class__()
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/
"Argue for your limitations, and sure enough they''re yours." --Richard Bach
这篇关于覆盖返回基类对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!