Java:如何使用clone()和演员检查呢

发布于 2025-02-08 09:25:16 字数 604 浏览 5 评论 0 原文

此代码:

class RawStringIterator {
        java.util.Stack<State> stateStack = new java.util.Stack<State>();
        RawStringIterator(RawStringIterator i) {
              stateStack = (java.util.Stack<State>) i.stateStack.clone();
        }
        /* ... */
}

给我这个警告:

Type safety: Unchecked cast from Object to Stack<Utils.OperatorTree.RawStringIterator.State>

我想我可以在这里忽略警告。但是我想知道如何使用 clone()通常?我是否总是必须使用 @suppresswarnings(“ UNCANCED”)每次使用 clone() ?还是我应该始终进行完全多余的额外检查?

This code:

class RawStringIterator {
        java.util.Stack<State> stateStack = new java.util.Stack<State>();
        RawStringIterator(RawStringIterator i) {
              stateStack = (java.util.Stack<State>) i.stateStack.clone();
        }
        /* ... */
}

gives me this warning:

Type safety: Unchecked cast from Object to Stack<Utils.OperatorTree.RawStringIterator.State>

I guess I can ignore the warning here. But I wonder about how to use clone() in general? Do I always have to use a @SuppressWarnings("unchecked") every time I use clone()? Or should I always do the completely redundant extra check?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

擦肩而过的背影 2025-02-15 09:25:16

如果您有选择的话,最好的是完全不是实现/使用 clone(),因为这是一个损坏的API 。只需实现 /使用复制构造函数即可。

如果出于某种紧迫的原因,您必须使用 clone()但可以更改其实现,请考虑声明 stack&lt; t&gt; .clone()才能返回 stack&lt; t&gt; 而不是对象 - 协变返回类型是合法的,因为Java5。

更新:如果 stack java.util.stack ,请考虑它的javadoc

Deque 接口及其实现,应优先使用此类。

和例如 a>提供复制构造函数

If you have the choice, the best is not to implement / use clone() at all, because it is a broken API. Just implement / use a copy constructor instead.

If for some pressing reason you must use clone() but can change its implementation, consider declaring Stack<T>.clone() to return Stack<T> instead of Object - covariant return types are legal since Java5.

Update: if the Stack in question is java.util.Stack, consider its Javadoc:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

And e.g. ArrayDeque provides a copy constructor.

于我来说 2025-02-15 09:25:16

没有办法避免这里的演员阵容。 clone()返回 object ,如果是 java.util.stack ,它不使用共同变化的返回类型。

如果这不是 java.util.stack ,则请勿实现 clone() - 很难正确正确。改为复制构造符。

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.

完美的未来在梦里 2025-02-15 09:25:16

您别无选择,只能忽略它。

虽然不直接相关(因为您没有编写 clone()方法),但此条目在 Java Generics常见问题可以很好地阅读(整个FAQ也是如此!)

You have very little choice but to ignore it.

Whilst not directly relevant (because you're not writing a clone() method), this entry in the Java Generics FAQ makes good reading (as does the whole FAQ!)

如果没结果 2025-02-15 09:25:16

是的,每次使用 clone()时,您需要明确抑制警告。

这是您可能希望使用复制构造函数而不是 clone()(如果有)的原因之一。

顺便说一句,当使用 RAWSTRINGITERATOR(RAWSTRINGITERATOR I)构造函数时,不必要的第一个初始化 stateStack 是不必要的:

class RawStringIterator {
    Stack<State> stateStack = new Stack<State>();
    RawStringIterator(RawStringIterator i) {
          stateStack = (Stack<State>) i.stateStack.clone();
    }
    /* ... */
}

您可能需要删除它。

Yes, you'll need to explicitly suppress the warnings each time you use clone().

This is one of the reasons you might prefer to use copy constructors instead of clone(), if available.

By the way in your code, when the RawStringIterator(RawStringIterator i) constructor is used, the first initialisation of stateStack is unnecessary:

class RawStringIterator {
    Stack<State> stateStack = new Stack<State>();
    RawStringIterator(RawStringIterator i) {
          stateStack = (Stack<State>) i.stateStack.clone();
    }
    /* ... */
}

You might want to remove that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文