reference - Are there any alternatives to implementing Clone in Java? -
In my Java project, I have different types of merchandise. These different types of merchandise are sub-classes of merchant class. Right now, I have a method that takes a trader as an argument and stores it 50 or more times in a vector. I'm having trouble because archiving 50 times the same object is to store 50 reference of the same object I need to store 50 copies of the object I apply But I do not want programmers to define a type of businessman being worried about being able to clone his class. Also, as mentioned, implementing a clone causes all kinds of problems. I do not think a copy maker will work either because if I define one in the trader class, then he did not know the kind of merchant who was copying and becoming just a general merchant. What can i do
EDIT: I really do not want to make accurate copies of a given object. What I'm really trying to do is to add a fixed number of vector to the vector. The problem is that the user needs to specify in the logic what kind of merchant he wants to add. Here's an example of what I'm trying to do: (Although my syntax is completely fictional)
Public Zero addTraders (* Merchant Type *) {tradervect.add (new * Merchant Type * ())} How can I get something like this in Java? Just add an abstract copy method. You can use the corporate return type so that the derivative type is a derivative version.
Public Interface Merchant {Trader Copy Trader (); ...} public finals class implements the MyTrader trademark {MyTrader copyTrader () {new mitrider back (this); } ...} Sometimes you want to behave with a collection of derivative types of merchants , which require cloning And then the properly typed collection. For this, you can use Generic with a phrase:
Public Interface Traders & lt; This Trader & gt; {This Copy Trader (); ...} public finals class MyTrader Traders apply & lt; MyTrader & gt; {Public Mitterrear Copy Trader () {New Miter Redred (This); } ...}
Comments
Post a Comment