简体   繁体   English

内部静态枚举作为泛型类型?

[英]Inner static enum as generic type?

I am learning and experimenting with Java generics and come up with following piece of code which does not compile as expected. 我正在学习和试验Java泛型,并提出了一段不能按预期编译的代码。 Result cannot be resolved . Result无法解决 I stands for input, O for output. I代表输入, O代表输出。

public interface Stats<I, O> {
    O addItem (int index, I item);
}

public class AStats implements Stats<Item, Result> {
    public static enum Result {
        SUCCESS1,
        SUCCESS2,
        ERROR;
    }

    @Override
    public Result addItem (int index, Item item) {
        //valid code
    }
}
  • Is there more elegant solution than declaring Result in a separate file? 有没有比在单独的文件中声明Result更优雅的解决方案?

  • Is it bad in general to have a method which returns an instance of generic type? 一般来说,有一个返回泛型类型实例的方法是不是很糟糕?

  1. Your classname is AStats.Result , not Result : 您的类名是AStats.Result ,而不是Result

     public class AStats implements Stats<Item, AStats.Result> { ... } 
  2. I don't think that returning an instance of generic, inner type is a bad idea. 我不认为返回泛型内部类型的实例是个坏主意。

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

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