无法将对象传递给单独 class 中的方法
Trouble passing an object to method in a separate class
所以我是第一次自己写 class,我唯一想不通的方法是 compareTo 方法,它应该有 "one parameter: a SavingsAccount object. Name it what you want."
public int compareTo(SavingsAccount secAccount)
{
int result;
if ( balance > secAccount.getBalance() )
result = 1;
else if ( balance == secAccount.getBalance() )
result = 0;
else
result = -1;
return result;
}
当我尝试编译时出现此错误:
错误:缺少 return 语句 }
在客户端(由我的教授编写,我不应该编辑)这是调用我的 compareTo 方法的行:
if ( savings1.compareTo(savings2) > 0 )
System.out.println("[client] Savings1 has the larger balance");
else if (savings1.compareTo(savings2) == 0 )
System.out.println("[client] Savings1 and Savings2 "
+ "have the same balance");
else
System.out.println("[client] Savings2 has the larger balance");
根据我的理解,参数 savings2 被传递给 compareTo,然后在我的 SavingsAccount class 中,参数是 secAccount 作为 SavingsAccount 对象。
您可以这样做,而不是声明一个 int 结果。
public int compareTo(SavingsAccount secAccount)
{
if ( balance > secAccount.getBalance() )
return 1;
else if ( balance == secAccount.getBalance() )
return 0;
else
return -1;
}
除非您需要那个结果变量,否则这将起作用。这样就不用写额外的代码设置变量的值了。
您可能只是有一个杂散的大括号。这是一个完整的工作示例:
文件内容 Test.java:
package com.jlb;
public class Test{
public static void main(String[] args)
{
SavingsAccount savings1 = new SavingsAccount(200);
SavingsAccount savings2 = new SavingsAccount(100);
if ( savings1.compareTo(savings2) > 0 )
System.out.println("[client] Savings1 has the larger balance");
else if (savings1.compareTo(savings2) == 0 )
System.out.println("[client] Savings1 and Savings2 "
+ "have the same balance");
else
System.out.println("[client] Savings2 has the larger balance");
}
}
文件内容 SavingsAccount.java:
package com.jlb;
public class SavingsAccount {
private int balance = 0;
public SavingsAccount(int amount){
this.balance = amount;
}
public int getBalance(){
return this.balance;
}
public int compareTo(SavingsAccount secAccount)
{
int result;
if ( balance > secAccount.getBalance() )
result = 1;
else if ( balance == secAccount.getBalance() )
result = 0;
else
result = -1;
return result;
}
}
您可以通过在创建 savings1 和 savings2 时更改值来测试它,然后 运行 它作为 Eclipse 中的 Java 程序或任何您喜欢的 IDE 程序。
所以我是第一次自己写 class,我唯一想不通的方法是 compareTo 方法,它应该有 "one parameter: a SavingsAccount object. Name it what you want."
public int compareTo(SavingsAccount secAccount)
{
int result;
if ( balance > secAccount.getBalance() )
result = 1;
else if ( balance == secAccount.getBalance() )
result = 0;
else
result = -1;
return result;
}
当我尝试编译时出现此错误:
错误:缺少 return 语句 }
在客户端(由我的教授编写,我不应该编辑)这是调用我的 compareTo 方法的行:
if ( savings1.compareTo(savings2) > 0 )
System.out.println("[client] Savings1 has the larger balance");
else if (savings1.compareTo(savings2) == 0 )
System.out.println("[client] Savings1 and Savings2 "
+ "have the same balance");
else
System.out.println("[client] Savings2 has the larger balance");
根据我的理解,参数 savings2 被传递给 compareTo,然后在我的 SavingsAccount class 中,参数是 secAccount 作为 SavingsAccount 对象。
您可以这样做,而不是声明一个 int 结果。
public int compareTo(SavingsAccount secAccount)
{
if ( balance > secAccount.getBalance() )
return 1;
else if ( balance == secAccount.getBalance() )
return 0;
else
return -1;
}
除非您需要那个结果变量,否则这将起作用。这样就不用写额外的代码设置变量的值了。
您可能只是有一个杂散的大括号。这是一个完整的工作示例:
文件内容 Test.java:
package com.jlb;
public class Test{
public static void main(String[] args)
{
SavingsAccount savings1 = new SavingsAccount(200);
SavingsAccount savings2 = new SavingsAccount(100);
if ( savings1.compareTo(savings2) > 0 )
System.out.println("[client] Savings1 has the larger balance");
else if (savings1.compareTo(savings2) == 0 )
System.out.println("[client] Savings1 and Savings2 "
+ "have the same balance");
else
System.out.println("[client] Savings2 has the larger balance");
}
}
文件内容 SavingsAccount.java:
package com.jlb;
public class SavingsAccount {
private int balance = 0;
public SavingsAccount(int amount){
this.balance = amount;
}
public int getBalance(){
return this.balance;
}
public int compareTo(SavingsAccount secAccount)
{
int result;
if ( balance > secAccount.getBalance() )
result = 1;
else if ( balance == secAccount.getBalance() )
result = 0;
else
result = -1;
return result;
}
}
您可以通过在创建 savings1 和 savings2 时更改值来测试它,然后 运行 它作为 Eclipse 中的 Java 程序或任何您喜欢的 IDE 程序。