使用默认构造方法对变量赋值。使用参数化构造方法,以传变量的值。定义一个方法,同时显示两个构造方法的值。怎么实现吖~

解决方案 »

  1.   

    class Test{
        String s;    Test(){
        }    Test(String str){
        s = str;
        }
        
        static void callConstructor(Test t){
            System.out.println(t.s);
        }
        
        public static void main(String[] args){
            callConstructor(new Test());
            callConstructor(new Test("with parameter"));
        }
    }输出:
    null
    with parameter
      

  2.   

    public class csdntest1{
    public static void main(String[] args){
    System.out.println(new test("3").outPram());
    }
    }class test{
    public test(){
    this.a = "1";
    }

    public test(String s){
    this();
    this.b = s;
    } public String outPram(){
    return (String) ("第一个构造函数:"+a+"\r\n"+"第二个构造函数:"+b);
    }
    private String a;
    private String b;
    }重载构造函数间调用问题...