how can I implement Comparable more than once?

后端 未结 3 810
情歌与酒
情歌与酒 2020-12-31 05:15

I\'m upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I\'ve been able to i

相关标签:
3条回答
  • 2020-12-31 05:48

    Having multiple implementations of generic interfaces would run into problems when you consider wildcards.

    This does not depend upon erasure.

    0 讨论(0)
  • 2020-12-31 05:51

    Generics don't exist after bytecode has been compiled.

    Restrictions from this: You can't implement / extend two or more interfaces / classes that would be same without the generic parameter and are different with the generic parameter.

    What you could do if you really really want type safety is:

    interface Foo<T extends Foo<?>> extends Comparable<T>
    interface Bar<T extends Bar<?>> extends Comparable<T>
    abstract class BarDescription<T extends Bar<?>> implements Bar<T>
    class FooBar extends BarDescription<FooBar> implements Foo<FooBar>
    
    0 讨论(0)
  • 2020-12-31 06:07

    I'd write a couple of Comparators and be done with it.

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