引用基类 [英] refering to base classes

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

问题描述

嗨 - 我对python很新,想知道是否有人可以帮助我理解

关于继承的事情。在这个简单的例子中,我怎么能修改''dog''的语音方法来调用基类''生物''

语音方法来自其中?


等级生物:

def __init __(自我):

self.noise =""

def voice(self):

return" voice:" + self.noise


class dog(生物):

def __init __(self):

self.noise ="树皮


def语音(个体经营):

打印支撑你的自我:


谢谢

glenn

解决方案

glenn写道:


hi - 我对python很新,想知道是否有人可以帮助我理解

这里有关于继承的东西。在这个简单的例子中,我怎么能修改''dog''的语音方法来调用基类''生物''

语音方法来自其中?


等级生物:

def __init __(自我):

self.noise =""

def voice(self):

return" voice:" + self.noise


class dog(生物):

def __init __(self):

self.noise ="树皮


def语音(个体经营):

打印支撑你的自我:


谢谢

glenn



试试这个:


类狗(生物):

.....

def语音(个体经营):

打印支撑你的自我:

生物。声音(个体经营)


应该这样做。


Chaz Ginger写道:


glenn写道:


> hi - 我对python很新,想知道是否有人可以帮助我理解
关于继承的事情。在这个简单的例子中,我怎么能修改''dog''的语音方法来调用基类''生物''语音方法呢?
class creature:
def __init __(self):
self.noise =""
def voice(self):
return" voice:" + self.noise

类狗(生物):
def __init __(self):
self.noise =" bark"

def voice (个体经营):
打印支撑你自己:

感谢
glenn



试试这个:


级狗(生物):

.....

def语音(个体经营):

打印支撑你自己:

creature.voice(个体经营)


应该这样做。



我忘记在''狗's''__ init__中提到你最好打电话

生物'__init__。你可能会看起来像这样:


def __init __(自我):

self.noise =''bark''

creature .__ init __(self)


还有另一种方法 - 使用Superclass - 但是我会把这个

练习留给读者。


glenn写道:


[...]在这个简单的例子中,我该如何修改语音方法

''dog''用它来调用基类''生物'的声音方法?


类生物:

def __init __(自我):

self.noise =""

def voice(self):

return" ;语音:" + self.noise


class dog(生物):

def __init __(self):

self.noise ="树皮


def语音(个体经营):

打印支撑你的自我:



如果你想让dog.voice()只打印voice:bark,你只需要省略

的语音方法对于狗类:它将继承自生物。


如果你想dog.voice()做其他事情,你可以调用超类''

这样的方法:


def语音(个体经营):

creature.voice(个体经营)

打印支撑你的自我

any_other_magic()


HTH

-

Roberto Bonvallet


hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of ''dog'' to call the base class ''creatures''
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bark"

def voice(self):
print "brace your self:"

thanks
glenn

解决方案

glenn wrote:

hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of ''dog'' to call the base class ''creatures''
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bark"

def voice(self):
print "brace your self:"

thanks
glenn

Try this:

class dog(creature):
.....
def voice(self):
print "brace your self:"
creature.voice(self)

This should do it.


Chaz Ginger wrote:

glenn wrote:

>hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of ''dog'' to call the base class ''creatures''
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bark"

def voice(self):
print "brace your self:"

thanks
glenn

Try this:

class dog(creature):
.....
def voice(self):
print "brace your self:"
creature.voice(self)

This should do it.

I did forget to mention that in ''dog"s'' __init__ you had better call
creature''s __init__. You might make it look like this:

def __init__(self):
self.noise = ''bark''
creature.__init__(self)

There is another approach - using Superclass - but I will leave that
exercise to the reader.


glenn wrote:

[...] In this trivial example, how could I modify the voice method of
''dog'' to call the base class ''creatures'' voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bark"

def voice(self):
print "brace your self:"

If you want dog.voice() to just print "voice: bark", you just have to omit
the voice method for the dog class: it will be inherited from creature.

If you want dog.voice() to do something else, you can call superclass''
method like this:

def voice(self):
creature.voice(self)
print "brace your self"
any_other_magic()

HTH
--
Roberto Bonvallet


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

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