Build A Generic Tree With Inheritance

前端 未结 3 1474
谎友^
谎友^ 2021-02-01 23:03

I am building a generic Tree class, which supports inheritance of sub-trees. But I\'ve encountered some problems. Would you please kindly help me?

3条回答
  •  执笔经年
    2021-02-01 23:45

    As I see it, there is no perfect solution to this problem. This is basically due to type erasure. The Erasure of Generic Methods article explains that your addChild(final Tree subTree) function will become a addChild(final Tree subTree) function. So, even if you could somehow have a generic parameter > addChild(final TreeType subTree) (not valid syntax!) it would be erased to addChild(final Tree subTree) at compile time. Adding your runtime test will work though, so the edit you made will do the job.

提交回复
热议问题