简体   繁体   English

如何覆盖将包含子类实例的 List 返回到原始方法 List 包含的类的方法?

[英]How can I override a method returning a List containing instances of subclass to the class the original methods List contained?

I realize the title is kind of messy, but that's the best I could come up with.我意识到标题有点乱,但这是我能想到的最好的。

Below is a minimal example of what I wish, but am currently failing to do.以下是我希望的一个最小示例,但目前无法做到。

public class ObjectA {}

public class ObjectB extends ObjectA {}

public interface HandlerInterface<T extends ObjectA> {

        public T easyToOverride();

        public List<T> hardToOverride();

}      

public class HandlerA implements HandlerInterface<ObjectA> {

        public ObjectA easyToOverride() {
                return new ObjectA();
        }

        public List<ObjectA> hardToOverride() {
                return new ArrayList<ObjectA>();
        }
}      

public class HandlerB extends HandlerA implements HandlerInterface<ObjectB> {

        /*
          This method ovverides its super method with ease since its return
          type directly inherits from the super class's return type
        */
        public ObjectB easyToOverride() {
                return new ObjectB();
        }

        /*
          This method is NOT accepted by the Java syntax since List<ObjectB>
          does NOT directly inherit from List<ObjectA>

          The method signature for hardToOverride() clashes with the signature
          in the super class and is not overridden because the return types
          don't obviously inherit each other
        */
         public List<ObjectB> hardToOverride() {
                return new ArrayList<ObjectB>();
        }
} 

Ignore that these classes should be in their own files and that I have not included their imports.忽略这些类应该在它们自己的文件中并且我没有包含它们的导入。 I just put it like this to make it easier to read.我只是这样写,以便于阅读。

As you may have understood by the comments in the class HandlerB , the method hardToOverride() is not accepted (throw this code into your IDE and watch it scream).正如您可能已经从HandlerB类中的注释中了解到,方法hardToOverride()不被接受(将此代码放入您的 IDE 并观看它尖叫)。

I realise I could just return a List<Object> and type cast the contents of the returned List object to the type that I personally know the specific handler instance returns ( ObjectA or ObjectB ), but that would mean that anyone using these methods has to understand the inner workings of them, and I do not like that.我意识到我可以只返回一个List<Object>并将返回的List对象的内容类型转换为我个人知道特定处理程序实例返回的类型( ObjectAObjectB ),但这意味着任何使用这些方法的人都必须了解他们的内部运作,我不喜欢那样。

What I want is to be able to override the List<ObjectA> hardToOverride() method with a method List<ObjectB> hardToOverride() without losing the hard typing that these methods provide.我想是能够覆盖List<ObjectA> hardToOverride()有方法的方法List<ObjectB> hardToOverride()不失辛苦打字,这些方法提供。

So my final question is:所以我的最后一个问题是:

Is there any way to keep all of these interfaces, inheritances and overrides without loosing the strong typing they provide in my example code?有没有办法保留所有这些接口、继承和覆盖,而不会失去它们在我的示例代码中提供的强类型?

If not, what is a better way to achieve a similar set of classes and interfaces that actually works?如果不是,那么实现一组实际工作的类似类和接口的更好方法是什么?

Your code is accepted if you declare HandlerA with a new generic even if this is never used really:如果您使用新的泛型声明 HandlerA,即使从未真正使用过,您的代码也会被接受:

public class HandlerA<T> implements HandlerInterface<ObjectA> {
//....
}

NOTE: This is to be considered just a workaround but as result your example code will work as you asked.注意:这只是一种解决方法,但因此您的示例代码将按您的要求工作。 Moreover, even if HandlerA declares a generic, you can anycase instantiate it also without brackets:此外,即使 HandlerA 声明了泛型,您也可以在任何情况下实例化它,而无需括号:

 ObjectA objectA = new HandlerA();

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

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