python的OOP问题 [英] python's OOP question

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

问题描述

有一个程序,它的结果是意外的aaa,我希望它是

预期的aaa。如何使其工作?

There''s a program, it''s result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

展开 | 选择 | Wrap | 行号

推荐答案

neoedmund写道:
neoedmund wrote:

有一个程序,它的结果是意外的aaa,我希望它是

预期的aaa。如何使其工作?

There''s a program, it''s result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

展开 | 选择 | Wrap | 行号


" neoedmund" < ne ******* @ gmail.comwrites:
"neoedmund" <ne*******@gmail.comwrites:

有一个程序,它的结果是意外的aaa,我希望它是

预期的aaa。如何使它工作?


[代码]


类C1(对象):

def v(自我,o):

返回预期+ o


类C2(对象):

def v( self,o):

返回意外+ o

def m(个体经营):

print self.v(" aaa)


C3级(对象):

def nothing(self):

pass


def test1():

o = C3()

setattr(o," m",C2()。m)

setattr(o," v",C1()。v)

om()
There''s a program, it''s result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

[code]

class C1(object):
def v(self, o):
return "expected "+o

class C2(object):
def v(self, o):
return "unexpected "+o
def m(self):
print self.v("aaa")

class C3(object):
def nothing(self):
pass

def test1():
o = C3()
setattr(o,"m",C2().m)
setattr(o,"v",C1().v)
o.m()



设置属性对象外部与

制作该对象的绑定方法不同。


在这种情况下,''o.m''是一个C2实例的绑定方法,并且没有知道C1的知识。 ''o.v''是C1实例的绑定方法,并且没有C2的b
知识。他们都不知道C3。


你想要实现的是什么?


-

\Unix是一个操作系统,OS / 2是操作系统的一半,|
` \ Windows是一个shell,而DOS是一个启动分区病毒。 - |

_o__)Peter H. Coffin |

Ben Finney

Setting attributes on an object externally isn''t the same thing as
making bound methods of that object.

In this case, ''o.m'' is a bound method of a C2 instance, and has no
knowledge of C1. ''o.v'' is a bound method of a C1 instance, and has no
knowledge of C2. Neither of them has any knowledge of C3.

What is it you''re trying to achieve?

--
\ "Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus." -- |
_o__) Peter H. Coffin |
Ben Finney


谢谢,凯。


但我需要一个动态的办法。假设我有一个现有的类,并从其他类中添加一些

方法。

Kay Schluehr写道:
thank you, Kay.

But i need a "dynamic" way. Say i have a existing class, and add some
method from other class into it.
Kay Schluehr wrote:

neoedmund写道:
neoedmund wrote:

有一个程序,它的结果是意外的aaa,我希望它是

预期aaa。如何使其工作?

There''s a program, it''s result is "unexpected aaa", i want it to be
"expected aaa". how to make it work?

展开 | 选择 | Wrap | 行号


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

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