2011-10-10 24 views
3

基本上我想檢查一個類是否是提供的接口的實例。檢入對象instanceof Class <?擴展Object2>

我有這個簽名的方法:

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?但他們沒不符合我的情況。

任何提示或想法?我知道我在某個地方犯了一個愚蠢的錯誤。

回答

5

我覺得你只是想

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.

+0

該訣竅。我知道這是一個愚蠢的錯誤。謝謝! – javydreamercsw