Method accepting two different types as parameter

前端 未结 8 1861
無奈伤痛
無奈伤痛 2020-12-13 00:39

I am writing a method that should accept as its parameter an object of one of two types which do not share a parent type other than Object. For example, the types are Dreams

8条回答
  •  不知归路
    2020-12-13 01:21

    As I'm using :

    void fooFunction(Object o){
    Type1 foo=null;
    if(o instanceof Type1) foo=(Type1)o;
    if(o instanceof Type2) foo=((Type2)o).toType1();
    // code
    }
    

    But that only works if Type2 can be converted to Type1

提交回复
热议问题