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

Java找不到符号get.Method,但声明了方法,java,Java,有人给我上了这门课,让我修正错误,用芬兰语补习这门课 对于本作业,您需要开发一个测试驱动程序和一个类似的类。幸运的是,Fuller教授已经编写了这样的类(大部分),您只需要添加一些代码并调试一些错误。 这是我所拥有的,也是我所得到的 public class Jedi implements Comparable { private String name; private double midi; public Jedi(String name, double mid

有人给我上了这门课,让我修正错误,用芬兰语补习这门课

对于本作业,您需要开发一个测试驱动程序和一个类似的类。幸运的是,Fuller教授已经编写了这样的类(大部分),您只需要添加一些代码并调试一些错误。 这是我所拥有的,也是我所得到的

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;
        }
    }
}
公共类绝地武士{
私有字符串名称;
私人双midi;
公共绝地(字符串名称,双midi){
this.name=名称;
this.midi=midi;
}
公共绝地(字符串名称){
this.name=名称;
this.midi=-1;
}
公共字符串toString(){
返回“绝地”+name+“\nMidi chlorians计数:“+midi+”\n”;
}
公共双getMidi(){
返回midi;
}
公共字符串getName(){
返回名称;
}
//如果对象的midi值相等,则返回true;否则返回false–代码不完整
公共布尔等于(绝地其他){
返回这个.getMidi()==other.getMidi();
}
//如果midi计数相等,则返回-1;如果midi计数相等,则返回1;如果midi计数相等,则返回0–代码不完整
公共整数比较(对象其他){
if(getMidi()other.getMidi()){
返回1;
}如果(这个等于(其他)){
返回0;
}
}
}
我一直得到一个找不到符号的方法
getMidi()

这有什么问题,因为我想不出来?

这就是问题所在:

public int compareTo(Object other)
您说过可以将此对象与任何其他对象进行比较。您不能调用
other.getMidi()
,因为
getMidi()
不是在
对象上声明的方法

我建议您更改类和方法声明,以使用泛型的事实:

public class Jedi implements Comparable<Jedi> {
    ...
    public int compareTo(Jedi other) {
        ...
    }
}
公共类绝地武士{
...
公共国际比较(绝地其他){
...
}
}
您的问题在于:

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;
}
  • 对象没有getMidi()方法

  • 如果不满足所有的if条件,将返回什么?所以请纠正这一点。如

  • public int compareTo(对象其他){
    if(getMidi()other.getMidi()){
    返回1;
    }如果(这个等于(其他)){
    返回0;
    }
    如果条件不满足,返回某物。
    }
    
    如果您试图使用在Object类中为Jedi类定义的方法,则无法使用。如果您需要管理具有
    对象
    类型的对象(您必须在强制转换之前确定其类型),在使用它之前,您必须将其强制转换为预期的对象类型(本例中为Jedi)

    因此,您的代码类似于以下代码:

    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 !!!
        }
    }
    
    公共类绝地武士{
    私有字符串名称;
    私人双midi;
    公共绝地(字符串名称,双midi){
    this.name=名称;
    this.midi=midi;
    }
    公共绝地(字符串名称){
    this.name=名称;
    this.midi=-1;
    }
    公共字符串toString(){
    返回“绝地”+name+“\nMidi chlorians计数:“+midi+”\n”;
    }
    公共双getMidi(){
    返回midi;
    }
    公共字符串getName(){
    返回名称;
    }
    //如果对象的midi值相等,则返回true;否则返回false–代码不完整
    公共布尔等于(绝地其他){
    返回这个.getMidi()==other.getMidi();
    }
    //如果midi计数相等,则返回-1;如果midi计数相等,则返回1;如果midi计数相等,则返回0–代码不完整
    公共整数比较(对象其他){
    if(getMidi()<((绝地)(其他)).getMidi()){
    返回-1;
    }else if(getMidi()>((绝地)(其他)).getMidi()){
    返回1;
    }如果(这个等于(其他)){
    返回0;
    }
    return-1;/!!!您没有提供此案例!!!
    }
    }
    

    请注意,您的
    compareTo()
    函数包含错误,您没有为主else分支提供
    return
    语句。

    您的compareTo方法存在问题。首先,你必须投掷你的另一个物体。第二,你必须把括号放在getMidi之后。
           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.
            }
    
    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 !!!
        }
    }