fork download
  1. class HelloWorld
  2. {
  3. public static void main(String[] args) {
  4.  
  5. Foo tmp = new Foo(10, 2);
  6.  
  7. Foo foo = (Foo)tmp.clone();
  8.  
  9. if (foo != null) {
  10. System.out.println(foo.getClass());
  11. System.out.println("foo.p = " + foo.getP());
  12. System.out.println("foo.n = " + foo.getN());
  13. System.out.println("foo.mul = " + foo.mul());
  14. } else {
  15. System.out.println("NULL!");
  16. }
  17.  
  18. System.out.println(tmp.equals(foo));
  19.  
  20. }
  21.  
  22. static class Hoge implements Cloneable {
  23.  
  24. int n;
  25. int p;
  26.  
  27. Hoge() {
  28. }
  29.  
  30. Hoge(int n, int p) {
  31. this.n = n;
  32. this.p = p;
  33. }
  34.  
  35. public Object clone() {
  36. try {
  37. System.out.println(this.getClass());
  38. Hoge h = this.getClass().newInstance();
  39. h.n = n;
  40. h.p = p;
  41. return h;
  42. } catch (Exception e) {
  43. return null;
  44. }
  45. }
  46.  
  47. public int getN() {
  48. return n;
  49. }
  50.  
  51. public int getP() {
  52. return p;
  53. }
  54. }
  55.  
  56. static class Foo extends Hoge {
  57.  
  58. Foo() {
  59. }
  60.  
  61. Foo(int n, int p) {
  62. super(n, p);
  63. }
  64.  
  65. public int mul() {
  66. return n * p;
  67. }
  68.  
  69. }
  70. }
  71.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
class HelloWorld$Foo
class HelloWorld$Foo
foo.p = 2
foo.n = 10
foo.mul = 20
false