So, here's what I can do in 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)
There are two problems I'm facing in the Java version of it:
- I have no clue how to create an instance of a class similar to Python's
__new__
method. - I want
Copiable
to be an interface, but then, I am unable to implement theclone()
method.
Can you think of a solution? Thanks