我可以继承成员变量吗? [英] Can I inherit member variables?

查看:68
本文介绍了我可以继承成员变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下想法:


类动物:

def __init __(自我,体重,颜色):

self.weight =重量

self.colour =颜色

类鸟类(动物):

def __init __(自我,翼展):

self.wingspan = wingspan

打印self.weight,self.colour,self.wingspan


class鱼(动物):

def __init __(自我,长度):

self.length = length

print self.weight,self.colour ,self.length

所以基本上我有一个具有某些属性的基类(动物)。

当动物被特定实例继承时,其他属性

已添加,但我需要访问原始属性(权重,

颜色)。当我从派生类中运行我的代码时,self.weight

和self.colour不会被继承(尽管方法是继承的,因为我预计会有
)。 br />

从阅读新闻组看来,解决方案似乎可能是
将重量和颜色声明为动物中的全局变量:

类动物:

通过


myanimal = animal()

myanimal.weight = 4
myanimal.colour =''blue''


但这不是我想做的事。


我我对OOP技术不是很有经验,所以也许我想要做的事情是不明智的。 Python的成员变量从C ++和Java继承是否有不同?


感谢您的帮助,


Lorcan。

I''m trying to work with the following idea:

class animal:
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour
class bird(animal):
def __init__(self, wingspan):
self.wingspan = wingspan
print self.weight, self.colour, self.wingspan

class fish(animal):
def __init__(self, length):
self.length = length
print self.weight, self.colour, self.length
So basically I have a base class (animal) that has certain attributes.
When animal is inherited by a specific instance, further attributes are
added, but I need to have access to the original attributes (weight,
colour). When I run my code from within the derived class, self.weight
and self.colour are not inherited (although methods are inherited as I
would have expected).

It seems from reading the newsgroups that a solution might be to
declare weight and colour as global variables in the class animal:

class animal:
pass

myanimal = animal()
myanimal.weight = 4
myanimal.colour = ''blue''

But this is not exactly what I want to do.

I''m not very experienced with OOP techniques, so perhaps what I''m
trying to do is not sensible. Does Python differ with regard to
inheritance of member variables from C++ and Java?

Thanks for any help,

Lorcan.

推荐答案

你好,

lm *** @ cam.ac.uk 写道:
Hello,

lm***@cam.ac.uk wrote:

我正在尝试使用以下内容想法:


类动物:

def __init __(自我,体重,颜色):

self.weight = weight

self.colour = color


class bird(animal):

def __init __(self,wingspan):

self.wingspan = wingspan

打印self.weight,self.colour,self.wingspan

类鱼(动物):

def __init __(自我,长度):

self.length =长度

打印self.weight,self.colour,self.length


所以基本上我有一个具有某些属性的基类(动物) />
当动物被特定实例继承时,进一步添加属性

,但我需要访问原始属性(重量,

颜色) )。当我从派生类中运行我的代码时,self.weight

和self.colour不会被继承(虽然方法是继承的,因为我会预期的b $ b)。
I''m trying to work with the following idea:

class animal:
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour
class bird(animal):
def __init__(self, wingspan):
self.wingspan = wingspan
print self.weight, self.colour, self.wingspan

class fish(animal):
def __init__(self, length):
self.length = length
print self.weight, self.colour, self.length
So basically I have a base class (animal) that has certain attributes.
When animal is inherited by a specific instance, further attributes are
added, but I need to have access to the original attributes (weight,
colour). When I run my code from within the derived class, self.weight
and self.colour are not inherited (although methods are inherited as I
would have expected).



你必须调用超类的__init__方法,这不是隐式完成的
。并且你可能想要为你的子类添加权重和颜色

属性,以便将它们传递给动物

构造函数。


类鱼(动物):

def __init __(自我,长度,重量,颜色):

animal .__ init __(自我,体重,颜色)

self.length = length

打印self.weight,self.colour,self.length

HTH


-

Benjamin Niemann

电子邮件:粉红色at odahoda dot de

WWW: http://pink.odahoda.de/




l ... @ cam.ac.uk写道:

l...@cam.ac.uk wrote:

当我从派生类中运行我的代码时,self.weight

和self.colour不是继承的(虽然方法是继承的,因为我会预期b $ b b)。
When I run my code from within the derived class, self.weight
and self.colour are not inherited (although methods are inherited as I
would have expected).



动物永远不会被初始化,无论如何你都没有传递重量和颜色

。你需要这样的东西:


类动物:#(对象):#< - 新式类

def __init __(自我,重量,颜色) :

self.weight =体重

self.colour =颜色


班鸟(动物):

def __init __(自我,体重,颜色,翼展):

#super(bird,self).__ init __(重量,颜色)#< - new-style init

animal .__ init __(自我,体重,颜色)#< - 旧式初学者

self.wingspan = wingspan

打印self.weight,self.colour ,self.wingspan


类鱼(动物):

def __init __(自我,重量,颜色,长度):

#super(fish,self).__ init __(重量,颜色)

animal .__ init __(自我,重量,颜色)

self.length = length

打印self.weight,self.colour,self.length

HTH,

Jordan

Animal is never initialized and you''re not passing weight and color
into it anyway. You need something like:

class animal: # (object): # <- new-style class
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour

class bird(animal):
def __init__(self, weight, color, wingspan):
#super(bird, self).__init__(weight, color) # <- new-style init
animal.__init__(self, weight, color) # <- old-style init
self.wingspan = wingspan
print self.weight, self.colour, self.wingspan

class fish(animal):
def __init__(self, weight, color, length):
#super(fish, self).__init__(weight, color)
animal.__init__(self, weight, color)
self.length = length
print self.weight, self.colour, self.length

HTH,
Jordan


感谢您的回复。


我认为对n有一个基本的误解

继承在我身边。


我想做的是从

实例化子类(派生类)在动物类中。然后我希望子类继承

它知道的一些基本属性(重量,颜色)。如果我可以

扩展我之前给出的例子,试图让它变得更清楚

更清晰:


类动物:

def __init __(自我,体重,颜色):

self.weight =体重

self.colour = color

def describeMyself(self,type,measurement):

如果type ==''bird'':

myanimal = bird(measurement)

elif type ==''fish'':

myanimal = fish(测量)

class bird(animal):
def __init __(自我,翼展):

self.wingspan = wingspan

打印我是鸟,重量%s,颜色%s ,wingspan%s %

(self.weight,self.colour,self.wingspan)


类鱼(动物):

def __init__ (自我,长度):

self.length = length

print"我是鱼,体重%s,颜色%s,长度%s" %(self.weight,

self.colour,self.length)

看来你所说的属性(成员变量)将是

必须像任何其他函数调用一样明确地传递。这个

当然是明智的,''bird''或''fish''与实例化的''动物'的特定

实例无关。


感谢您的帮助,

Lorcan。

Benjamin Niemann写道:

Thanks for the reply.

I think there''s a basic misunderstanding about the nature of
inheritance on my side.

What I want to do is instantiate the sub class (derived class) from
within the animal class. I then expect the sub class to have inherited
some basic properties that it knows it has (weight, colour). If I can
expand the example I gave previously to try to make this a little
clearer:

class animal:
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour

def describeMyself(self, type, measurement):
if type == ''bird'':
myanimal = bird(measurement)
elif type == ''fish'':
myanimal = fish(measurement)

class bird(animal):
def __init__(self, wingspan):
self.wingspan = wingspan
print "I''m a bird, weight %s, colour %s, wingspan %s" %
(self.weight, self.colour, self.wingspan)

class fish(animal):
def __init__(self, length):
self.length = length
print "I''m a fish, weight %s, colour %s, length %s" % (self.weight,
self.colour, self.length)
It seems from what you say that the attributes (member variables) will
have to be passed forward explicitly like any other function call. This
of course is sensible, ''bird'' or ''fish'' are not tied to a specific
instance of ''animal'' when they are instantiated.

Thanks for the help,
Lorcan.
Benjamin Niemann wrote:


你必须调用超类的__init__方法,这不是隐式完成的
。并且你可能想要为你的子类添加权重和颜色

属性,以便将它们传递给动物

构造函数。


类鱼(动物):

def __init __(自我,长度,重量,颜色):

animal .__ init __(自我,体重,颜色)

self.length = length

print self.weight,self.colour,self.length
You''ll have to invoke the __init__ method of the superclass, this is not
done implicitly. And you probably want to add the weight and colour
attributes to your subclass in order to pass these to the animal
constructor.

class fish(animal):
def __init__(self, length, weight, colour):
animal.__init__(self, weight, colour)
self.length = length
print self.weight, self.colour, self.length


这篇关于我可以继承成员变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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