java: how to use clone() and what about the cast check

前端 未结 4 997
小蘑菇
小蘑菇 2020-12-11 08:14

This code:

class RawStringIterator {
        java.util.Stack stateStack = new java.util.Stack();
        RawStringIterator(RawStrin         


        
4条回答
  •  生来不讨喜
    2020-12-11 08:37

    There is no way to avoid the cast here. clone() returns Object, if it is java.util.Stack, it is not making use of co-variant return types.

    If this is not java.util.Stack, then don't implement clone() - it's really hard to get it right. Make a copy-constructor instead.

提交回复
热议问题