小编Ang*_*mar的帖子

Java:为什么我的 toString 方法打印错误信息?

我有一个抽象超类,它有两个属性:int 和 string。我已经覆盖了其中的 toString 方法及其具有一个额外属性 (LocalDate) 的子类。但是,由于某种我不明白的原因,当我打印子类 toSring 信息时,int 值会发生变化。

这是我在超类中的内容:

public abstract class File {
private int id;
private String text;

public File(int newId, String newText) throws IllegalArgumentException {
      id(newId);
      text(newText);
}

public int id() {
   return id;
}

public void id(int e) throws IllegalArgumentException {      
   if (e <= 0) {
      throw new IllegalArgumentException();
   }
   else {
      id = e;
   }
}

public String text() {
   return text;
}

public void text(String aText) throws IllegalArgumentException {
   if (aText == …
Run Code Online (Sandbox Code Playgroud)

java overriding subclass tostring superclass

3
推荐指数
1
解决办法
147
查看次数

标签 统计

java ×1

overriding ×1

subclass ×1

superclass ×1

tostring ×1