简体   繁体   English

将对象传递给单独类中的方法时遇到麻烦

[英]Trouble passing an object to method in a separate class

So I am writing my own class for the first time and the only method that I have not been able to figure out is the compareTo method which is supposed to have "one parameter: a SavingsAccount object. Name it what you want." 因此,我是第一次编写自己的类,而我一直无法弄清的唯一方法是compareTo方法,该方法应该具有“一个参数:SavingsAccount对象。将其命名为所需的名称”。

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;
}

When I try to compile i get this error: 当我尝试编译时,出现此错误:
error: missing return statement } 错误:缺少return语句}

in the client (written by my professor, which I am not supposed to edit) this is the line that calls on my compareTo method: 在客户端(由我的教授写的,我不应该编辑)中,这是调用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");

From what I understand the argument savings2 is being passed to compareTo and then in my SavingsAccount class, the parameter is secAccount as an object SavingsAccount. 据我了解,将参数Savings2传递给compareTo,然后在我的SavingsAccount类中,该参数是secAccount作为对象SavingsAccount。

Instead of declaring a int result you can do something like this. 无需声明int结果,您可以执行以下操作。

public int compareTo(SavingsAccount secAccount)
{
    if ( balance > secAccount.getBalance() )
        return 1;
    else if ( balance == secAccount.getBalance() )
        return 0;
    else
        return -1;
}

Unless you need that result variable this will work. 除非您需要该结果变量,否则它将起作用。 There's no need to write extra code setting values of variables when you can just do it this way. 只要您可以这样做,就无需编写额外的变量代码设置值。

You may just have a stray curly brace. 您可能只是流浪的花括号。 Here is a complete working example: 这是一个完整的工作示例:

Contents of file Test.java: 文件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");
    }


}

Contents of file SavingsAccount.java: 文件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;
    }
}

You can test it by changing the values when you create savings1 and savings2, then run it as a Java program in Eclipse or whatever your favorite IDE is. 您可以通过在创建Savings1和Savings2时更改值来进行测试,然后在Eclipse或您喜欢的任何IDE中将其作为Java程序运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM