简体   繁体   English

我的System.out.println(); 保持打印null而不是应打印的文本

[英]My System.out.println(); keeps printing null instead of the text it should print

I started Java a while ago and I am now busy with abstract classes. 我前一段时间启动了Java,现在我正忙于抽象类。 I've been busy with this code for two days now and I can't find out how to fix this. 我已经忙了两天的代码了,我找不到解决方法。 The methods names might not be in English, sorry for that but I think it wouldn't be too big of a problem. 方法名称可能不是英文,对此感到抱歉,但是我认为这不是一个太大的问题。 I don't know why the output is as following: null null null null null null null null null null null null null null null null 我不知道为什么输出如下:null null null null null null null null null null null null null null null null null null null null null

I hope you guys can help me out. 我希望你们能帮助我。 I would appreciate any help! 我将不胜感激任何帮助! Thanks in advance. 提前致谢。

Main class, which is also abstract: 主类,它也是抽象的:

public abstract class Boek {
public String isbn;
public String auteur;
public String paginas;
public String titel;

public abstract void setIsbn(String isbn);
public abstract void getIsbn(); 
public abstract void setAuteur(String auteur);
public abstract void getAuteur();
public abstract void setPaginas(int paginas);
public abstract void getPaginas();
public abstract void setTitel(String titel);
public abstract void getTitel();

public static void main(String[] args) {
    Studieboek  sb = new Studieboek();
    Roman       ro = new Roman();
    Dichtbundel db = new Dichtbundel();

    sb.setAuteur("J.K. Rowling");
    sb.setIsbn("547896587412");
    sb.setPaginas(251);
    sb.setTitel("Harry Potter");
    sb.addNAuteur("R.K. Dale");
    sb.addOndertitel("Exactly");
    sb.printSB();

    ro.setAuteur("Suzanne Vermeer");
    ro.setIsbn("9632589632574");
    ro.setPaginas(311);
    ro.setTitel("De Zwarte Piste");
    ro.printRO();

    db.setAuteur("A.Y. Lmao");
    db.setIsbn("5781236985478");
    db.setPaginas(171);
    db.setTitel("Rijmen kreng");
db.addGedicht("Rijmpje");
    db.printDB();
}
}

First subclass: 第一子类:

public class Studieboek extends Boek {  
private String ondertitel;
private String nAuteur;

@Override
public void setIsbn(String isbn) {

}

@Override
public void getIsbn() {

}

@Override
public void setAuteur(String auteur) {

}

@Override
public void getAuteur() {

}

@Override
public void setPaginas(int paginas) {

}

@Override
public void getPaginas() {

}

@Override
public void setTitel(String titel) {

}

@Override
public void getTitel() {

}

public void printSB() {
    System.out.println(titel + " " + auteur + " " + paginas + " " + isbn + " " + ondertitel + " " + nAuteur);
}

public void addOndertitel(String ondertitel) {

}

public void addNAuteur(String nAuteur) {

}

I have two more subclasses after this but I don't think it is necessary for the code to work, because the code in both other subclasses are exactly the same and give the exact same output. 此后我还有两个子类,但是我认为代码没有必要工作,因为其他两个子类中的代码完全相同,并且给出的输出完全相同。

Again, any help is appreciated. 再次感谢您的帮助。 Thanks in advance. 提前致谢。

Sincerely, 此致

Double

Your setters aren't doing anything. 您的二传手什么也没做。

A correct way to implement eg your setter for isbn would be: 例如,实现isbn的设置器的正确方法是:

@Override
public void setIsbn(String isbn) {
    this.isbn = isbn;
}

Note the part where it actually sets something. 请注意它实际设置的部分。

Your getters are equally wrong. 您的吸气剂同样是错误的。 A getter should return something, ie not be declared as void and have a return statement: 一个吸气剂应该返回一些东西,即不被声明为void并有一个return语句:

@Override
public String getIsbn() {
    return this.isbn;
}

I know they should contain a return value, but the return value gives an error and whenever I leave it empty it seems fine. 我知道它们应该包含一个返回值,但是该返回值给出了一个错误,每当我将其保留为空时,它似乎都很好。 (from the comments) (从评论)

I'm guessing the error is something along "a void method cannot return anything" and yes, it will be fine - unless you try something like 我猜错误是“无效方法无法返回任何东西”的东西,是的,这很好-除非您尝试类似

System.out.println(sb.getIsbn()); // Will also print "null"

I'll leave the rest up to you. 我把剩下的交给你。


Bonus 1: If you have an abstract parent class anyway, you can move the common getters and setters to Boek instead of just declaring them as abstract . 奖励1:如果仍然有一个抽象的父类,则可以将常见的getter和setter移到Boek而不仅仅是将它们声明为abstract This will save you from having to re-implement them in each of your subclasses again. 这将使您不必再在每个子类中重新实现它们。

Bonus 2: public fields (as in public String isbn; ) are usually discouraged. 奖励2:通常不鼓励使用public字段(例如public String isbn; )。 You already have the getters and setters, so make them protected (see also: encapsulation ). 您已经有了吸气剂和吸气剂,因此要使其protected (另请参见: 封装 )。

Bonus 3: As already pointed out by Mike 'Pomax' Kamermans in the comments: You don't want to have your main method in your Boek class. 奖励3:正如Mike'Pomax'Kamermans在评论中所指出的那样:您不想在Boek类中拥有main方法。 Create a separate "application" class with only the main method to start up your application. 仅使用main方法创建一个单独的“ application”类来启动您的应用程序。

Bonus 4: I believe the standard way of what you want to achieve with your printSB , printRO and printDB methods would be to override the toString() method. 奖金4:我相信您要使用printSBprintROprintDB方法实现的标准方法是覆盖toString()方法。 Although this might be different for your special use case. 尽管对于您的特殊用例这可能有所不同。

Use it as following: 使用它如下:

public class Studieboek extends Boek {
    ...

    @Override
    public String toString() {
        return this.isbn + " " + this.auteur; // Plus the others
    }
}

And

// No need to call an additional method, toString will be invoked automatically
System.out.println(db);

Your set and get methods are empty. 您的set和get方法为空。 You need to set a value in the setters and return the value in the getters . 您需要在setters设置一个值,然后在getters返回该值。

Right now you are trying to print a variable that has not yet been set - so yes, it is going to print null . 现在,您正在尝试打印尚未设置的变量-是的,它将打印null

public class FooClass{
    private String foo;

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getFoo() {
        return foo;
    }
}

You are printing the variable, whose values have not set. 您正在打印未设置其值的变量。 You have set the variable values in setter methods in order to print the values. 您已在setter方法中设置了变量值以打印值。 Setter methods to set the values. 设置值的设置方法。 And getter methods to return the values. 和getter方法返回值。

@Override
public void setTitel(String titel) {
     this.titel=titel;
}

@Override
public String getTitel() {
   return titel;
}

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

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