How to justify why an unchecked cast is okay, regarding Copyable getObjectCopy()

后端 未结 1 836
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 00:06

(This is a follow-up to my previous question.)

I have an interface called Copyable, which has a single function

Copyable getObjectCopy();


        
相关标签:
1条回答
  • 2021-01-21 00:27

    We are in Java 5+ world now! User Generics.

    You can change the signature of Copyable to something like:

    interface Copyable<T extends Copyable> {
        T getObjectCopy();
    }
    

    Now your ValidateValue<L> value would be something like:

    puvlic class ValidateValue<L> implements Copyable<ValidateValue<L>> {
        ...
    }
    

    and everyone (including the compiler) would be happy!

    0 讨论(0)
提交回复
热议问题