简体   繁体   English

Java找不到符号get.Method,但声明了Method

[英]Java Cannot find symbol get.Method but Method is declared

I was given this class and told to fix errors and Finnish the class. 给我上了这堂课,并告诉我解决错误并完成这堂课。

For this assignment, you will need to develop a test driver and a comparable class. 对于此作业,您将需要开发一个测试驱动程序和一个类似的类。 Luckily, Prof. Fuller has such class already written (well most of it), you just need to add some code and debug some errors. 幸运的是,富勒教授已经编写了此类课程(大部分课程),您只需要添加一些代码并调试一些错误即可。 This is what I have as well as what was given. 这就是我所拥有的以及所给予的。

public class Jedi implements Comparable {

    private String name;
    private double midi;

    public Jedi(String name, double midi) {
        this.name = name;
        this.midi = midi;
    }

    public Jedi(String name) {
        this.name = name;
        this.midi = -1;
    }

    public String toString() {
        return "Jedi " + name + "\nMidi-chlorians count : " + midi + "\n";
    }

    public double getMidi() {
        return midi;
    }

    public String getName() {
        return name;
    }

    // returns true if the object’s midi value are equal and false otherwise – CODE INCOMPLETE
    public boolean equals(Jedi other) {
        return this.getMidi() == other.getMidi();
    }

    // returns -1 if less, 1 if larger, or 0 if it is an equal midi count – CODE INCOMPLETE
    public int compareTo(Object other) {
        if (getMidi() < other.getMidi()) {
            return -1;
        } else if (getMidi > other.getMidi()) {
            return 1;
        } else if (this.equals(other)) {
            return 0;
        }
    }
}

I keep getting a cannot find symbol - method getMidi() 我不断收到找不到符号-方法getMidi()

What is wrong with this because I can not figure it out? 因为我无法弄清楚这有什么问题?

This is the problem: 这就是问题:

public int compareTo(Object other)

You've said you can compare this object to any other object . 您已经说过可以将此对象与任何其他对象进行比较。 You can't call other.getMidi() , because getMidi() isn't a method declared on Object . 您不能调用other.getMidi() ,因为getMidi()不是在Object声明的方法。

I would suggest you change your class and method declarations to use the fact that Comparable<T> is generic: 我建议您更改类和方法声明,以使用Comparable<T>是泛型的事实:

public class Jedi implements Comparable<Jedi> {
    ...
    public int compareTo(Jedi other) {
        ...
    }
}

Your problem is here: 您的问题在这里:

public int compareTo(Object other)
{
    if (getMidi() < other.getMidi())
        return -1;
    else if (getMidi > other.getMidi())
        return 1;
    else if (this.equals(other))
        return 0;
}

other is an Object . other是一个Object An Object does not have a getMidi() method. Object没有getMidi()方法。 Change it to: 更改为:

public int compareTo(Jedi other)
{
    if (getMidi() < other.getMidi())
        return -1;
    else if (getMidi > other.getMidi())
        return 1;
    else if (this.equals(other))
        return 0;
}
  1. Object does not have a getMidi() method. 对象没有getMidi()方法。

  2. What if all the if conditions are not met, what is returned? 如果不满足所有前提条件,会返回什么? So please correct that. 所以请改正。 As in

       public int compareTo(Object other) {

            if (getMidi() < other.getMidi()) {
                return -1;
            } else if (getMidi > other.getMidi()) {
                return 1;
            } else if (this.equals(other)) {
                return 0;
            }
            return ....Something if conditions are not met.
        }

You are trying to use the methods that you defined for the Jedi class in the Object class, this can not work. 您正在尝试使用为Object类中的Jedi类定义的方法,这是行不通的。 If you need to manage an object that has an Object type (you have to be sure about its type before to cast it), before to use it you have to cast it to the expected object type (Jedi for this example). 如果您需要管理具有Object类型的Object (在转换之前必须先确定其类型),则在使用它之前,必须将其转换为所需的对象类型(此示例为Jedi)。

So, your code would be similar to the following one: 因此,您的代码将类似于以下代码:

public class Jedi implements Comparable {
    private String name;
    private double midi;

    public Jedi(String name, double midi) {
        this.name = name;
        this.midi = midi;
    }

    public Jedi(String name) {
        this.name = name;
        this.midi = -1;
    }

    public String toString() {
        return "Jedi " + name + "\nMidi-chlorians count : " + midi + "\n";
    }

    public double getMidi() {
        return midi;
    }

    public String getName() {
        return name;
    }

    // returns true if the object’s midi value are equal and false otherwise – CODE INCOMPLETE
    public boolean equals(Jedi other) {
        return this.getMidi() == other.getMidi();
    }

    // returns -1 if less, 1 if larger, or 0 if it is an equal midi count – CODE INCOMPLETE
    public int compareTo(Object other) {
        if (getMidi() < ((Jedi)(other)).getMidi()) {
            return -1;
        } else if (getMidi() > ((Jedi)(other)).getMidi()) {
            return 1;
        } else if (this.equals(other)) {
            return 0;
        }
        return -1; // !!! YOU DID NOT PROVIDE THIS CASE !!!
    }
}

Please note that your compareTo() function contains an error, you do not provide a return statement for the main else branch. 请注意,您的compareTo()函数包含错误,您没有为main else分支提供return语句。

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

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