我试着运行一个程序,但我得到了这四个错误。
TestCusomer.java:25:错误:发票中的toString()不能覆盖对象中的toString() (与第49行相同)
和
第59行找不到符号。myCustomer.setTrn(112233778) 第60行找不到符号。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);
}
}发布于 2015-03-27 00:00:49
很少的东西,
toString方法为公共toString方法中返回一个字符串cannot find symbol...,是因为这些方法不是在Customer中定义的,而是在Invoice中定义的。发布于 2015-03-26 23:59:56
您的toString()方法需要返回字符串对象。您正在输出它们中的字符串,但不返回字符串。也让他们公开。
例如,发票类的toString()方法应该是:
public String toString()
{
return trn + " : " + persentage;
}对于第二个问题(找不到符号),这些方法在发票类中而不是在Customer类中,因此不能在Customer对象上调用它们。
https://stackoverflow.com/questions/29290993
复制相似问题