我尝试运行一个程序,但出现这四个错误。
TestCusomer.java:25: error: toString() in Invoice cannot override toString() in Object (same thing at line 49)
和
line 59 cannot find symbol . myCustomer.setTrn(112233778)
line 60 cannot find symbol . myCustomer.setPersentage(150)
我的程序如下:
class Invoice
{
int trn; //TAX REGISTRATION NUMBER
int persentage;
public Invoice{}
public int setTrn(int trn){
this.trn = trn;
}
public int getTrn(){
return trn;
}
public void setPersentage(int persentage){
this.persentage = persentage;
}
public int getPersentage(){
return persentage;
}
String toString(){
System.out.println(trn+" : "+persentage);
}
}
class Customer{
int trn;
int charging= 0;
public Customer(int trn){
this.trn = trn;
}
public int charge(int amount){
charging = charging + amount;
}
public int charge(int amount , int trn){
if (this.trn == trn){
charging = charging + amount;
}
}
String toString(){
System.out.println(trn+" : "+charging);
}
}
class TestCustomer
{
public static void main(String[] args){
Customer myCustomer = new Customer(112233778);
myCustomer.charge(100);
myCustomer.setTrn(112233778);
myCustomer.setPersentage(150);
System.out.println(myCustomer);
}
}
最佳答案
几件事,
- 您需要将
toString
方法声明为 public - 您需要在
toString
方法中返回一个字符串 - 我强烈建议您添加 @Override重写方法时的表示法以确保您确实重写了该方法
- 发生
找不到符号...
是因为这些方法未在Customer
中定义,您在Invoice
中定义了这些方法
关于java - 修复覆盖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290993/