为什么我的方法未为此类型定义?

Why is my method undefined for this type?

我不确定为什么 Eclipse 会给我这个错误,我无法 运行 任何我想要的 类 的交换方法:

The method exchange(Currency, double) is undefined for the type Currency

由于我的编译问题,列出的一些代码方法尚未完全实现。

我犯了什么简单的错误?

public abstract class Currency {
  String currencyName;
  double totalFunds;

  //Exchange money between planets
  public abstract double toEarthDollars(double amount);

  public abstract double fromEarthDollars(double EarthDollars); 

  public static void main(String[] args) {
    //TEST INPUT CODE
    Currency mars = new Mars(100.00);
    Currency neptune = new Neptune(100.00);
    Currency saturn = new Saturn(100.00);

    System.out.println("----- Exchanges -----");

    mars.exchange(saturn,25.0);
    neptune.exchange(saturn, 10.0);
    saturn.exchange(mars, 122.0);
    saturn.exchange(mars, 121.0);
  }
} 


public interface Exchangeable {
  //rates should be encapsulated and accessed from here
  double EarthDollar = 1;
  double MarsMoney = 1.3;
  double SaturnSilver = 0.87;
  double NeptuneNuggets = 2;

  public void exchange(Exchangeable other, double amount);
}


public class Mars extends Currency implements Exchangeable {
  public Mars(double amount) {
    currencyName = "MarsMoney";
    totalFunds = amount;
  }

  public double toEarthDollars(double amount) {
    return amount * (Exchangeable.EarthDollar/Exchangeable.MarsMoney);
  }

  public double fromEarthDollars(double EarthDollars) {
    return EarthDollars * (Exchangeable.MarsMoney/Exchangeable.EarthDollar);
  }

  public void exchange(Exchangeable other, double amount) { 
    System.out.println("Hello");
    //double transfer = this.toEarthDollars(amount);
    //transfer = ((Mars) other).fromEarthDollars(transfer);
  }
}

您的抽象 Currency class 没有实现 Exchangeable 接口。变化

public abstract class Currency {

public abstract class Currency implements Exchangeable {

我认为您的 Currency class 不知道 exchange 的方法,只有 Exchangeable 有。

所以你可能想要投射:

  Currency mars = new Mars(100.00);
  Exchangeable marsEx = (Exchangeable)mars;
  Exchangeable saturnEx = (Exchangeable)saturn;
  marsEx.exchange(saturnEx,25.0);

您可能还想检查 Currency 是否可以转换为 Exchangeable,因为可能并非所有货币都可以兑换。

您可能还需要施放 saturn,因为 Currency 无法传递给交换方法。