Java: returning subclass in superclass method signature

后端 未结 3 735
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 23:04

I\'m working on a problem where there are several implementations of Foo, accompanied by several FooBuilder\'s. While Foo\'s share sev

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 23:37

    Generics might be the way to go here.

    If you declare setA() something like this (pseudo-code)

     T setA(int a)
    

    the compiler should be able to figure out the real types, and if it doesn't you can give hints in the code like

    obj.setA(42)
    

提交回复
热议问题