Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public class Test {
  2.  
  3. public void test() throws CloneNotSupportedException {
  4. T t = new T();
  5. t.setI(10);
  6. t.setJ(20);
  7.  
  8. T cloned = (T)t.clone();
  9. System.out.println(cloned.getI());
  10. System.out.println(cloned.getJ());
  11. }
  12.  
  13.  
  14. public static void main(String[] args) throws CloneNotSupportedException {
  15. new Test().test();
  16. }
  17.  
  18. class T implements Cloneable {
  19.  
  20. private int i = 0;
  21. private int j = 0;
  22.  
  23. public int getI() {
  24. return i;
  25. }
  26.  
  27. public void setI(int i) {
  28. this.i = i;
  29. }
  30.  
  31. public int getJ() {
  32. return j;
  33. }
  34.  
  35. public void setJ(int j) {
  36. this.j = j;
  37. }
  38.  
  39. @Override
  40. public Object clone() throws CloneNotSupportedException {
  41. super.clone();
  42. T clone = new T();
  43. clone.setI(getI());
  44. clone.setJ(getJ());
  45. return clone;
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment