Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can try to use reflection:</p> <pre><code>public abstract class AClonable implements Cloneable{ private String val; public AClonable(){ } public AClonable(String s){ val=s; } public String toString(){ return val; } @Override public AClonable clone(){ try { System.out.println(getClass().getCanonicalName()); AClonable b= getClass().getDeclaredConstructor(String.class).newInstance(val); return b; } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } </code></pre> <p>}</p> <p>in the clone() method you call getClass(). Because the ACloneble ist abstract, there call will allways go to the concrete class.</p> <pre><code> public class ClonebaleOne extends AClonable{ public ClonebaleOne(){ super(); } public ClonebaleOne(String s) { super(s); // TODO Auto-generated constructor stub } </code></pre> <p>}</p> <p>and </p> <pre><code> public class ClonebaleTwo extends AClonable{ public ClonebaleTwo(){ super(); } public ClonebaleTwo(String s) { super(s); // TODO Auto-generated constructor stub } </code></pre> <p>}</p> <p>and finally</p> <pre><code> public static void main(String[] args){ AClonable one = new ClonebaleOne("One"); AClonable tow= new ClonebaleTwo("Two"); AClonable clone = one.clone(); System.out.println(clone.toString()); clone = tow.clone(); System.out.println(clone.toString()); } </code></pre> <p>Output:</p> <pre><code> ClonebaleOne One ClonebaleTwo Two </code></pre> <p>But it's more a hack than a solution</p> <p>[EDIT] my two clones were faster than ;)</p> <p>[EDIT] To be complete. Another implentation of clone() can be</p> <pre><code> @Override public AClonable clone(){ try { ByteArrayOutputStream outByte = new ByteArrayOutputStream(); ObjectOutputStream outObj = new ObjectOutputStream(outByte); ByteArrayInputStream inByte; ObjectInputStream inObject; outObj.writeObject(this); outObj.close(); byte[] buffer = outByte.toByteArray(); inByte = new ByteArrayInputStream(buffer); inObject = new ObjectInputStream(inByte); @SuppressWarnings("unchecked") Object deepcopy = inObject.readObject(); inObject.close(); return (AClonable) deepcopy; } catch (Exception e) { e.printStackTrace(); } return null; } </code></pre> <p>when your abstract class implements Serialazable. There you write your object to disc and create a copy with the value from the disc. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload