简体   繁体   English

为什么我得到 null 作为字符串 output?

[英]Why do I get null as the String output?

Why does the String variable outputs String as the Name and the Building Material?为什么字符串变量输出字符串作为名称和建筑材料?

Always when I am printing out my code, the output at the Name and the building material is always "null".当我打印出我的代码时,名称中的 output 和建筑材料总是“空”。

public class Mauer{

    private int height;
    private int width;
    private String baumaterial;
    private String Name;

    Mauer(int h, int b, String bm, String n){ 
        this.setD(h,b, baumaterial, Name); 
        }

    public void setD(int h, int b, String bm, String n) {
      this.height=h;
      this.width=b;
      this.baumaterial = bm;
      this.Name = n;


    }

    public int getH() {
        return height; 
        }
    public int getW() { 
        return width; 
        }
    public String getBM() {
        return baumaterial;
        }
    public String getN() {
        return Name;
    }
}

... and the second part ...和第二部分

public class Program extends Mauer{

    Program(int h, int b, String bm, String n) {
        super(h, b,  bm, n);
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        Mauer m1 = new Mauer(5,10, "Stein", "Burgmauer");
        Mauer m2 = new Mauer(7,12, "Holz", "Holzmauer");

        System.out.println("Die Höhe der Mauer ist " + m1.getH());
        System.out.println(" Die Breite der Mauer ist "+ m1.getW());
        System.out.println(" Das Material besteht aus "+ m1.getBM());
        System.out.println("Der Name der Mauer ist "+m1.getN());

        System.out.println("Die Höhe der zweiten Mauer ist "+ m2.getH());
        System.out.println(" Die Breite der zweiten Mauer ist "+ m2.getW());
        System.out.println(" Das zweite Material besteht aus "+ m2.getBM());
        System.out.println("der Name der zweiten Mauer 2 ist "+ m2.getN());

    }

}

your constructor is passing as parameters to a method the empty variables that it has.您的构造函数将其具有的空变量作为参数传递给方法。 Yo should pass哟应该通过

 this.setD(h, b, bm, n)

Name for example was only defined but not initialised so Name still is null, and you were passing It as an argument to setD.例如,名称仅定义但未初始化,因此名称仍然是 null,并且您将它作为参数传递给 setD。 If you do debug you will see this.如果您进行调试,您将看到这一点。

In Java, all instance variables are initialized to their default values before the constructor has a chance to initialize them.在 Java 中,所有实例变量在构造函数有机会初始化它们之前被初始化为其默认值。 For objects, the default value is null .对于对象,默认值为null

Your constructor for the Mauer class passes not the arguments bm and n , but the instance variables baumaterial and Name . Mauer class 的构造函数不传递 arguments bmn ,而是传递实例变量baumaterialName (Normal Java naming conventions would name Name as name , lowercase.) When setD is called, it essentially sets these variables to themselves -- null . (正常的 Java 命名约定将Namename ,小写。)当setD被调用时,它实际上将这些变量设置为它们自己 - null

When these values are retrieved for printing, the + operator performs string conversion, which converts null to the string "null" , which you see as output.当检索这些值以进行打印时, +运算符执行字符串转换,将null转换为字符串"null" ,您将看到该字符串为 output。

Fix your constructor to pass all 4 parameters to setD instead of just 2.修复您的构造函数以将所有 4 个参数传递给setD而不是仅传递 2 个。

Incidentally, there is no reason apparent in the code given in this question for Program to extend Mauer .顺便说一句,在这个问题中给出的代码中没有明显的理由让Program扩展Mauer It's likely that you can remove the Program constructor and have Program not extend Mauer .您可能可以删除Program构造函数并使Program不扩展Mauer

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

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