【问题标题】:overriding interface method重写接口方法
【发布时间】:2012-01-26 15:31:39
【问题描述】:

对于诸如

这样的接口
public interface something<T>
{
   public something<T> somemethod();
}

据我了解,抽象方法 somemethod() 需要用返回实现接口的对象的方法覆盖。但是,任何这样做的尝试都给了我“不覆盖抽象方法 somemethod()”的编译器错误。

我试过做类似的事情

public class someclass {
...
    public something<T> somemethod() { ... return new someclass(); }
...
or 
    public someclass somemethod() { ... return new someclass(); }
...
}

我将如何实现这样的方法?

【问题讨论】:

    标签: java generics inheritance interface


    【解决方案1】:

    您的实现类中缺少通用声明。这是一个例子:

    public interface Something <T> {
    
        public Something<T> someMethod();
    
    }
    
    class SomethingImplementation <T> implements Something <T> {
    
        @Override
        public Something<T> someMethod() {
            return null;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      所有这些都应该编译:

      class test<T> implements something<T>{
          public something<T> somemethod(){
              return new test<T>();
          }
      }
      
      class test1<T> implements something<T>{
          public test1<T> somemethod(){
              return new test1<T>();
          }
      }   
      
      class test2 implements something<Integer>{
          public something<Integer> somemethod(){
              return new test2();
          }
      }
      
      class test3 implements something<Integer>{
          public test3 somemethod(){
              return new test3();
          }
      }
      

      【讨论】:

        【解决方案3】:

        首先,您的someclass 没有实现上面代码示例中的接口。您可以使其实现特定的具体类型的接口,如下例所示,其中String 用作具体类型。然后该方法需要返回一个something&lt;String&gt;

        public class someclass implements something<String> {
            public something<String> somemethod() {
                return new someclass();
            }
        }
        

        或者你可以让 someclass 类有一个类型参数并将其用于接口:

        public class someclass<X> implements something<X> {
            public something<X> somemethod() {
                return new someclass<X>();
            }
        }
        

        【讨论】:

          【解决方案4】:

          据我了解,somemethod() 需要的抽象方法 被一个方法覆盖,该方法返回一个实现 界面。

          这是不正确的。方法somemethod需要返回一个something&lt;T&gt;

          【讨论】:

            【解决方案5】:

            你还需要用泛型扩展接口,比如:

            公共类 someclass 实现了一些东西 { 公开某事方法(){ ... } }

            【讨论】:

              【解决方案6】:

              要覆盖一个方法,你必须实现一个具有相同类型的参数和相同类型的返回值的方法。
              String something(){}
              将被String something(){}覆盖
              但不适用于char[] something(){}String something(int){}

              【讨论】:

                【解决方案7】:

                据我了解,你需要实现接口的东西,你可以通过简单的方式做到这一点:

                public class someclass<T> implements something<T> {
                    public something<T> somemethod() { ... return new someclass(); }
                }
                

                【讨论】:

                  【解决方案8】:
                  public class someclass<T> extends something<T> {
                  
                  public something<T> somemethod() { ... return new someclass<T>(); }}
                  

                  【讨论】:

                    猜你喜欢
                    • 2019-03-04
                    • 2012-04-09
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-03-22
                    • 2019-01-24
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多