接口和参数问题

Issue with interface and parameter

我的界面和参数有问题:

public class Functions {
  public static Double getInfoSum(TreeMap<String, Iinterface> map){
    ....some counting
  }
}

public class ExampleClass {

  private TreeMap<String, ExampleClassItem > xxxx;

  public static void SomeFunction(){
    Functions.getInfoSum(xxxx); //HERE IS ERROR 
  }
}


public class ExampleClassItem implements Iinterface {

  .....

}

除了这个还有其他的可能吗:

private TreeMap<String, ExampleClassItem > xxxx; -----> private TreeMap<String, Iinterface > xxxx;

因为我需要使用 ExampleClassItem 有一些特定的功能不能在界面中。

你可以改变

public static Double getInfoSum(TreeMap<String, Iinterface> map){
 ....some counting
}

public static Double getInfoSum(TreeMap<String, ? extends Iinterface> map){
 ....some counting
}

这将允许您将 TreeMap<String, ExampleClassItem> 传递给此方法。

首先

 public static void SomeFunction(){
   Functions.getInfoSum(xxxx); //HERE IS ERROR 
 }

你的 "xxxx" 不是静态的,你的方法是静态的,所以你不能在你的方法中使用非静态字段。

你可以改变你的方法,使用“?”通配符

public static Double getInfoSum(TreeMap<String, ? extends Iinterface> map){

所以你可以在这里传递任何扩展 Iinterface 的东西。

ExampleClassItem 是 Iinterface 的子类型 但是 TreeMap<String, ExampleClassItem>

不是 TreeMap<String, Iinterface>

的子类型