难以理解继承...... [英] trouble understanding inheritance...
问题描述
这不符合我的想法....
似乎fromfile和getName正在调用baseClass
方法
简单传递....我做错了什么?
class baseClass:
def __init __(self,type):
如果type ==''A'':
self = typeA()
else:
self = typeB ()
def fromfile(self):
pass
def getName(self):
pass
class typeA(baseClass):
def __init __(self):
self.name =''A''
print''typeA init''
def fromfile(self):
print''typeA fromfile''
def getName(self):
print self.name
class typeB(baseClass):
def __init __(self):
self.name =''B''
打印''typeB init''
def fromfile(self):
打印''typeB fromfile''
d ef getName(self):
print self.name
a = baseClass(''A'')
a.fromfile ()
a.getName()
b = baseClass(''B'')
b.fromfile()
b.getName()
日志:
typeA init
typeB init
This is not working the way I think it should....
it would appear that fromfile and getName are calling the baseClass
methods which are
simple passes.... What have I done wrong?
class baseClass:
def __init__(self, type):
if type == ''A'':
self = typeA()
else:
self = typeB()
def fromfile(self):
pass
def getName(self):
pass
class typeA(baseClass):
def __init__(self):
self.name=''A''
print ''typeA init''
def fromfile(self):
print ''typeA fromfile''
def getName(self):
print self.name
class typeB(baseClass):
def __init__(self):
self.name=''B''
print ''typeB init''
def fromfile(self):
print ''typeB fromfile''
def getName(self):
print self.name
a = baseClass(''A'')
a.fromfile()
a.getName()
b = baseClass(''B'')
b.fromfile()
b.getName()
log:
typeA init
typeB init
推荐答案
KraftDiner写道:
KraftDiner wrote:
这不符合我的想法....
看来fromfile和getName正在调用baseClass
方法
简单传递....什么有我错了吗?
class baseClass:
def __init __(self,type):
if type ==''A '':
self = typeA()
else:
self = typeB()
This is not working the way I think it should....
it would appear that fromfile and getName are calling the baseClass
methods which are
simple passes.... What have I done wrong?
class baseClass:
def __init__(self, type):
if type == ''A'':
self = typeA()
else:
self = typeB()
__init__不是构造函数,并且赋值给self并没有改变构造对象的
类型。
lo好像你需要得到一个更好的教程。
< / F>
__init__ is not a constructor, and assigning to self doesn''t change the
type of the constructed object.
looks like you need to get a better tutorial.
</F>
Fredrik Lundh写道:
Fredrik Lundh wrote:
KraftDiner写道:
KraftDiner wrote:
这不是我的工作方式认为它应该....
似乎fromfile和getName正在调用baseClass
方法
简单传递...我做错了什么?
class baseClass:
def __init __(self,type):
如果type == ''A'':
self = typeA()
else:
self = typeB()
This is not working the way I think it should....
it would appear that fromfile and getName are calling the baseClass
methods which are
simple passes.... What have I done wrong?
class baseClass:
def __init__(self, type):
if type == ''A'':
self = typeA()
else:
self = typeB()
__init__不是构造函数,并且赋值给self并没有改变构造对象的
类型。
看起来你需要一个更好的教程。
< / F>
__init__ is not a constructor, and assigning to self doesn''t change the
type of the constructed object.
looks like you need to get a better tutorial.
</F>
当你用b $ b构建对象时,如何选择哪个类baseClass?
什么是我错过了吗?
a = typeA()
b = typeB()
c = baseClass(a)
Well how does one select which class baseClass really is when you
contruct the object?
What am I missing?
a = typeA()
b = typeB()
c = baseClass(a)
KraftDiner写道:
KraftDiner wrote:
Fredrik Lundh写道:
Fredrik Lundh wrote:
KraftDiner写道:
KraftDiner wrote:
这不符合我认为应该的方式....
看起来像fromfile和getName正在调用baseClass
方法
简单传递....我做错了什么?
>
class baseClass:
def __init __(self,type):
如果type ==''A'':
self = typeA()
else:
self = typeB()
This is not working the way I think it should....
it would appear that fromfile and getName are calling the baseClass
methods which are
simple passes.... What have I done wrong?
>
class baseClass:
def __init__(self, type):
if type == ''A'':
self = typeA()
else:
self = typeB()
__init__不是构造函数,并且分配自我没有改变构造对象的
类型。
看起来你需要得到一个更好的教程。
< / F>
__init__ is not a constructor, and assigning to self doesn''t change the
type of the constructed object.
looks like you need to get a better tutorial.
</F>
当你用b $ b构建对象时,如何选择哪个类baseClass?
什么是我错过了吗?
a = typeA()
b = typeB()
c = baseClass(a)
Well how does one select which class baseClass really is when you
contruct the object?
What am I missing?
a = typeA()
b = typeB()
c = baseClass(a)
a = typeA()
b = typeB()
你已经完成了。停在那里。
你不能选择哪个类基类确实是 - 它真的是
baseClass。你选择你的对象是哪一类是通过选择用于构造对象的
类。
HTH,
~Simon
a = typeA()
b = typeB()
You''re done. Stop there.
You can''t "select which class baseClass really is"-- it really is
baseClass. You "select" which class your object is by choosing which
class to use to construct the object.
HTH,
~Simon
这篇关于难以理解继承......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!