如何使用匿名对象函数和覆盖方法将 Java 转换为 Python
How to Convert Java to Python using anonymous object function and overriding a method
还有其他 Whosebug questions 与此类似,但不是针对我需要重写函数的问题。
我想将下面的Java代码转换成Python:
import Someclass;
Someclass obj = new Someclass("stringarg") {
@Override
void do(x) {
// Do extra stuff on function
super.do(x);
}
}
我是否需要为此明确创建一个虚拟 class?可以仅在像这样的 Java 代码的单个语句中将其转换为 Python 吗?
二手:Python 2.7.6,Java 1.8
P.S。这将在 Jython 中完成,其中 Someclass
是导入的 Java 库。
如有任何帮助,我们将不胜感激。谢谢。
更新:
- 对于那些和我有同样问题的人,但是导入 class 是一个抽象 class:
感谢 DNA 的 link,我设法通过在他的方法中添加一个 super 语句来使其工作,如下所示:
def patch(self, x):
# do something
print 'goodbye'
super(Someclass, self).do(x)
obj = type('DummySomeclass', (Someclass, object), {"do": patch})("hello")
对于那些 import class 不是抽象的人,DNA 和 algrebe 的代码已经可以工作,但也只是添加了缺失的 super:
...
super(Someclass, self).do(x)
如果我没理解错的话,你有一个带方法的 class,你需要重写一个函数,而不是继承这个 class 并重写它。
Monkey Patching 是一种解决方法。
Your code may be different, but Python gives you good tools to avoid action at a distance. You can use closures, decorators, even sometimes selectively monkey patching modules. Take advantage of the fact that Python is a dynamic language, with strong support for metaprogramming, and remember that the Jython implementation makes these techniques accessible when working with even recalcitrant Java code.
class SomeClass(object):
def __init__(self):
self.a = "a"
def foo(self):
print "Base Foo"
def bar(self):
print "Base bar"
# new foo should execute something and then call the existing one
# i take it this is why you needed "super"
oldfoo = SomeClass.foo
def newFoo(self, name="default name"):
print "Overridden %s" % name
oldfoo(self)
# completely replace bar
def newBar(self):
print "Overridden bar"
SomeClass.foo = newFoo
SomeClass.bar = newBar
obj = SomeClass()
obj.foo()
obj.bar()
如果您想 override/patch 特定对象 的方法(而不是 class 级别,如 algrebe 的回答中所述),您可以做到这一点,但你需要注意方法是否正确绑定,如this article
中所述
这是一个可运行的例子:
import types
class Someclass():
def __init__(self, x):
self.x = x
def do(self):
print self.x
if __name__ == "__main__":
def patch(self):
print "goodbye"
obj = Someclass("hello")
obj.do = types.MethodType(patch, obj)
obj.do() # prints "goodbye"
还有其他 Whosebug questions 与此类似,但不是针对我需要重写函数的问题。
我想将下面的Java代码转换成Python:
import Someclass;
Someclass obj = new Someclass("stringarg") {
@Override
void do(x) {
// Do extra stuff on function
super.do(x);
}
}
我是否需要为此明确创建一个虚拟 class?可以仅在像这样的 Java 代码的单个语句中将其转换为 Python 吗?
二手:Python 2.7.6,Java 1.8
P.S。这将在 Jython 中完成,其中 Someclass
是导入的 Java 库。
如有任何帮助,我们将不胜感激。谢谢。
更新:
- 对于那些和我有同样问题的人,但是导入 class 是一个抽象 class:
感谢 DNA 的 link,我设法通过在他的方法中添加一个 super 语句来使其工作,如下所示:
def patch(self, x):
# do something
print 'goodbye'
super(Someclass, self).do(x)
obj = type('DummySomeclass', (Someclass, object), {"do": patch})("hello")
对于那些 import class 不是抽象的人,DNA 和 algrebe 的代码已经可以工作,但也只是添加了缺失的 super:
... super(Someclass, self).do(x)
如果我没理解错的话,你有一个带方法的 class,你需要重写一个函数,而不是继承这个 class 并重写它。 Monkey Patching 是一种解决方法。
Your code may be different, but Python gives you good tools to avoid action at a distance. You can use closures, decorators, even sometimes selectively monkey patching modules. Take advantage of the fact that Python is a dynamic language, with strong support for metaprogramming, and remember that the Jython implementation makes these techniques accessible when working with even recalcitrant Java code.
class SomeClass(object):
def __init__(self):
self.a = "a"
def foo(self):
print "Base Foo"
def bar(self):
print "Base bar"
# new foo should execute something and then call the existing one
# i take it this is why you needed "super"
oldfoo = SomeClass.foo
def newFoo(self, name="default name"):
print "Overridden %s" % name
oldfoo(self)
# completely replace bar
def newBar(self):
print "Overridden bar"
SomeClass.foo = newFoo
SomeClass.bar = newBar
obj = SomeClass()
obj.foo()
obj.bar()
如果您想 override/patch 特定对象 的方法(而不是 class 级别,如 algrebe 的回答中所述),您可以做到这一点,但你需要注意方法是否正确绑定,如this article
中所述这是一个可运行的例子:
import types
class Someclass():
def __init__(self, x):
self.x = x
def do(self):
print self.x
if __name__ == "__main__":
def patch(self):
print "goodbye"
obj = Someclass("hello")
obj.do = types.MethodType(patch, obj)
obj.do() # prints "goodbye"