同一类中构造函数互调

    科技2024-10-14  20

    同一类中构造函数互调

    public class Demo01 { private String name; private int num; Demo01(String a,int b){ name=a; num=b; } Demo01(String a){ this(a,0); } Demo01(int b){ num=b; } Demo01(){ this("Unknown"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class Test { public static void main(String[] args) { Demo01 obj = new Demo01(); Demo01 obj1 = new Demo01("hehao",123); Demo01 obj2 = new Demo01("xxxx"); Demo01 obj3 = new Demo01(666); System.out.println(obj.getName()); System.out.println(obj1.getName()); System.out.println(obj2.getName()); System.out.println(obj3.getName()); System.out.println(obj.getNum()); System.out.println(obj1.getNum()); System.out.println(obj2.getNum()); System.out.println(obj3.getNum()); } } Unknown hehao xxxx null 0 123 0 666
    Processed: 0.060, SQL: 8