问题描述
对不起我的无知,但在阅读了关于
python.org的Python教程之后,我有点惊讶关于python中缺少OOP
的能力。老实说,我甚至都没有看到关于OO如何在Python中实际工作的重点。
对于一个,有什么好的理由可以解释为什么我应该继承
班吗? ^^没有功能可以检查子类是否正确
实现了一个继承的接口,并且多态现象似乎也在Python中缺失了
。我无法想象在哪种情况下使用Python中的
继承有帮助。例如:
类基数:
def foo(个体经营):#我想说儿童必须实施foo
通过
class Child(Base):
pass#works
Python中的继承是否沸腾仅仅是代码共享?
我如何在Python中制定多态?例如:
class D1(Base):
def foo(self):
print" D1"
D2级(基础):
def foo(个体经营):
打印" D2"
obj = Base()#我想要一个多态的基类引用
if(< need D1>):
obj = D1()
其他:
obj = D2()
我还可以把整个继承的东西都拿出来并且程序
仍然可以工作(?)。
请给我希望Python仍然值得学习: - /
问候,
Matthias
Hi,
sorry for my ignorance, but after reading the Python tutorial on
python.org, I''m sort of, well surprised about the lack of OOP
capabilities in python. Honestly, I don''t even see the point at all of
how OO actually works in Python.
For one, is there any good reason why I should ever inherit from a
class? ^^ There is no functionality to check if a subclass correctly
implements an inherited interface and polymorphism seems to be missing
in Python as well. I kind of can''t imagine in which circumstances
inheritance in Python helps. For example:
class Base:
def foo(self): # I''d like to say that children must implement foo
pass
class Child(Base):
pass # works
Does inheritance in Python boil down to a mere code sharing?
And how do I formulate polymorphism in Python? Example:
class D1(Base):
def foo(self):
print "D1"
class D2(Base):
def foo(self):
print "D2"
obj = Base() # I want a base class reference which is polymorphic
if (<need D1>):
obj = D1()
else:
obj = D2()
I could as well leave the whole inheritance stuff out and the program
would still work (?).
Please give me hope that Python is still worth learning :-/
Regards,
Matthias
推荐答案
让这个启发你的方式,年轻的padawan:
modelnine @ phoenix 〜/ gtk-gnutella-downloads
Let this enlighten your way, young padawan:
modelnine@phoenix ~/gtk-gnutella-downloads
并且,_do_(不只是阅读)教程,你会明白为什么你发布的
简短示例代码并不是pythonic,至少可以说:
以及为什么Python中的继承是必要的,但是在一个完全不同的层面上
你在想什么。
哦,最后一点,如果你是德国人,你可以加入
de.comp.lang.python。
--- Heiko。
And, _do_ (don''t only read) the tutorial, and you''ll understand why the
short example code you posted isn''t pythonic, to say the least:
http://www.python.org/doc/2.4.2/tut/tut.html
and why inheritance in Python is necessary, but on a whole different level
of what you''re thinking.
Oh, and on a last note, if you''re german, you might as well join
de.comp.lang.python.
--- Heiko.
QOTW!
< / F>
QOTW!
</F>
这篇关于Python中的OO? ^^的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!