java构造函数中的逻辑围绕不同的变量

时间:2016-08-01 16:07:37

标签: java variables constructor

我有3个java程序,其中使用了不同的变量,它给出了相同的输出。但是,我无法获得程序内部和外部使用的变量的逻辑。如果你有时间的话,请帮忙。

1

class A {
    static String name1;

    A(String name1) {
        this.name1=name1;
    }
}

public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

2

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}


public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

3

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}

public class Nam extends A {
    Nam(String p) {
        super(p);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

1 个答案:

答案 0 :(得分:0)

1

class A {
    static String name1; //static field "name1" of type String

    A(String name1) { //constructor of "A" with a String parameter
        this.name1=name1; //pass the value of the parameter "name1"
                          //to the static field "name1"
    }
}

public class Nam extends A {
    Nam(String name1) { //constructor of "Nam" with a String param
        super(name1); //call the constructor of "A" with the parameter
                      //passed to this constructor ("name1" 1 line above)
    }

    public static void main(String args[]) { //main function of the app
        Nam ob=new Nam("hai"); //create 1 object of Type "Nam" passing
                               //to the "Nam" constructor the string "hai"
        System.out.println(name1); //print the value of the static field "name1"
    }
}

2

class A {
    static String name1;
    A(String a) {
        this.name1=a; //same as in nr 1, only now the local value
                      //that is passed is called "a" inside the constructor
    }
}


public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

3

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}

public class Nam extends A {
    Nam(String p) { //again, the local name changed to "p"
                    //it only matters inside this constructor
        super(p);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

问:在第三个问题中,在Nam类中给予p的值如何反映在A类中的不同变量a

答:与nr 1和nr 2中的方式完全相同。在nr 3中,它被称为p,但它可以是任何东西,只要它在函数标题和正文中是相同的,例如:

Nam(String functionParameter) {
    super(functionParameter);
}

一步一步,它看起来像这样:

  1. Nam ob=new Nam("hai");使用字符串“hai”
  2. 调用Nam构造函数
  3. Nam(String functionParameter) {所以现在functionParameter成为对“hai”的引用
  4. super(functionParameter);使用字符串“hai”
  5. 调用A构造函数
  6. A(String a) {现在a成为“hai”
  7. 的引用
  8. this.name1=a;最后,静态字段a成为对字符串“hai”的引用