gpt4 book ai didi

java - checkin 对象 instanceof Class

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:23 24 4
gpt4 key购买 nike

基本上我想检查一个类是否是所提供接口(interface)的实例。

我有一个带有这个签名的方法:

public ICard draw(Class<? extends ICardType> type)

然后我尝试这样做,但它被标记为错误;

if (deck.get(i) instanceof type)

NetBeans 将此作为错误给出:

cannot find symbol
symbol: class type location: class simple.marauroa.client.extension.cardgame.impl.DefaultDeck

我什至从其他问题之一尝试过:

deck.get(i).isAssignableFrom(type)

我看到类似这样的问题:Checking programmatically if a .class file extends particular classHow to check instanceof on an argument that is a Class object?但它们不符合我的情况。

有什么提示或想法吗?我知道我在某个地方犯了一个愚蠢的错误。

最佳答案

我想你只是想要

if (type.isInstance(deck.get(i)))

来自documentation :

Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

关于java - checkin 对象 instanceof Class<?扩展对象 2>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7714992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com