如果我从同一个包中调用此实例方法,为什么需要 public 修饰符?
public String toString() {
if (isOn()) {
return "Speed is " + speed + ", color is " + color
+ " and radius is " + radius;
}
else
return "Fan is off. " + "Color is " + color
+ " and radius is " + radius;
}
最佳答案
因为您实际上是在尝试覆盖 Object.toString()
。
由于该方法是公共(public)的,因此您的可访问性必须匹配。
关于java - 为什么在重写 toString 方法时需要 public 修饰符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976264/