繁体   English   中英

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

[英]logic in java constructor around different variables

我有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个

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”调用Nam构造函数
  2. Nam(String functionParameter) {因此,现在functionParameter成为对“ hai”的引用
  3. super(functionParameter); 用字符串“ hai”调用A构造函数
  4. A(String a) { ,现在a成为对“ hai”的引用
  5. this.name1=a; 最后,静态字段a成为对字符串“ hai”的引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM