How to Inherit method but with different return type?

前端 未结 10 1052
北荒
北荒 2021-01-17 19:20

Given the following classes:

ClassA
{
     public ClassA DoSomethingAndReturnNewObject()
     {}    
}

ClassB : ClassA
{}

ClassC : ClassA
{}
10条回答
  •  没有蜡笔的小新
    2021-01-17 19:41

    There's one really simply answer: make all the method return types "object". You're obviously returning different types, strings and ints and whatnots, and they will stay what they are after they reach their destination. But the compiler is perfectly happy - everything's an "object". You do give up compile-time type-checking, not a good thing, but every once in a while for some deep OO programming the return is worth it.

提交回复
热议问题