zoukankan      html  css  js  c++  java
  • 新建对象:反射会调用构造函数,clone不会调用构造函数

    class Ins implements java.lang.Cloneable { 

        public Ins() {
            System.out.println("Construct called");

        }  

        @Override
        public Ins clone() throws CloneNotSupportedException {
            return (Ins) super.clone();
        }

    }


    public class ConstructTest {

     @Test
        public void test() throws InstantiationException, IllegalAccessException, CloneNotSupportedException {
            System.out.println("\n---new Ins() begin---");
            Ins ins1 = new Ins(); 
            System.out.println("---new Ins() end---");
            System.out.println("\n---Ins.class.newInstance() begin---");
            Ins ins2 = Ins.class.newInstance(); 
            System.out.println("---Ins.class.newInstance() end---");
            System.out.println("\n---ins1.clone() begin---");
            Ins ins3 = ins1.clone(); 
            System.out.println("---ins1.clone() end---");
        }

    }


    输出:

    ---new Ins() begin---
    Construct called
    ---new Ins() end---

    ---Ins.class.newInstance() begin---
    Construct called
    ---Ins.class.newInstance() end---

    ---ins1.clone() begin---
    ---ins1.clone() end---

  • 相关阅读:
    一些动规题
    洛谷P1717 钓鱼
    一堆递推题
    义冢oj P5033打气球
    义冢oj P5032生理周期
    Proud Merchants HDU
    739B
    Lost Cows POJ
    并查集负值根表集合大小的写法
    [Poi2011]Tree Rotations线段树合并
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276763.html
Copyright © 2011-2022 走看看