0
所以,这里是我可以在Python做:Java的可复制的对象,类似这样的Python的例子
class Copiable(object):
def copy_from(self, other):
""" This method must be implemented by subclasses to define their
own copy-behaviour. Never forget to call the super-method. """
pass
def copy(self):
""" Creates a new copy of the object the method is called with. """
instance = self.__new__(self.__class__)
instance.copy_from(self)
return instance
class Rectangle(Copiable):
def __init__(self, x, y, w, h):
super(Rectangle, self).__init__()
self.x = x
self.y = y
self.w = w
self.h = h
# Copiable
def copy_from(self, other):
self.x = other.x
self.y = other.y
self.w = other.w
self.h = other.h
super(Rectangle, self).copy_from(self)
有我面对的Java版本的这两个问题:
- 我不知道如何创建类似Python的
__new__
方法的类的实例。 - 我想
Copiable
是一个接口,但是,我无法执行clone()
方法。
你能想出一个解决方案吗?谢谢