2013年12月18日星期三

Help us to see clone on the JPanel ( ) problem

Look at the code :
public class CommonCondtionPanel extends JPanel implements Cloneable
{
public Object clone ()
{
CommonCondtionPanel condition = null;
try
{
condition = (CommonCondtionPanel) super.clone ();
}
catch (Exception e)
{
e.printStackTrace ();
}
return condition;
}
public CommonCondtionPanel getConditionPanel (CommonCondtionPanel condition)
throws Exception
{
CommonCondtionPanel conditionPanel = (CommonCondtionPanel) condition.clone ();
}
}


In another class, I want to make multiple calls CommonCondtionPanel:

CommonCondtionPanel condition = new CommonCondtionPanel ();
{
Test test1 = new Test (condition.getConditionPanel (condition));
}
{
Test test2 = new Test (condition.getConditionPanel (condition));

}

always throws java.lang.CloneNotSupportedException:
online explanation: This clone method
Object class entirely for service subclasses override it , not let subclasses called directly , which is why when you override clone method is generally to call super.clone () causes. clone method can be overridden , but must implement the interface Cloneable , so after you cover , it does not call the parent class method , but after calling the method you covered .

solving paste the code to understand the best , thanks to gray often

------ Solution ------------------------------------ --------
fact, personally feel that you need not have to write , of course , so you have to write , I'll try.
------
I try to analyze your program , in addition to a full complement of your program , but found that , in fact, your program is correct ! ,
Really, your program is correct.
you do not believe , consider the following a complete imitation of your program :
package com;

import javax.swing.JPanel;

public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
A a = new A() ;
Test t1 = new Test(a.getConditionA(a)) ; 
Test t2 = new Test(a.getConditionA(a)) ; 
Test t3 = new Test(a.getConditionA(a)) ; 
Test t4 = new Test(a.getConditionA(a)) ; 
}
Test(A a ){

}
}

class A extends JPanel implements Cloneable{
@Override
public Object clone() throws CloneNotSupportedException {
A a = null ;
a = (A)super.clone();
return a ;
}
public A getConditionA(A condition) throws CloneNotSupportedException{
A a = (A) condition.clone() ;
return a;
}
}

new Test (a.getConditionA (a)); This one is also called four times, no problem !
--------------
1, maybe you do not care where their program was wrong , right ?
2, in fact, this program does not make sense for you , for example , assume that I am here to clone is a lot of objects associated with a class , then to deep copy ,
Can your program completely failed it!
I personally think so, it would be better to resolve with the flow , when much more convenient, although the efficiency will be lower ...
------ For reference only --- ------------------------------------
top up , and study
--- --- For reference only ---------------------------------------
problem has solved, I did not use clone () method , because I instantiate an object called multiple times , only the last one will work , but then I re-add a click event in a tree , learn, thank you very much

没有评论:

发表评论