标签: instanceof

'instanceof'的例子

public class TableModel2 extends TableModel1 { ... }

TableModel2 tableModel = new TableModel2();

boolean t1 = tableModel instanceof TableModel1;
boolean t2 = tableModel instanceof TableModel2;
Run Code Online (Sandbox Code Playgroud)

在上述例子中,t1t2true.那么,我如何区分TableModel1TableModel2使用instanceof

java instanceof

20
推荐指数
4
解决办法
7万
查看次数

instanceof Class <?>参数

我正在尝试编写一个方法来返回与它获取的类匹配的所有对象作为参数:

public class Scenario extends View {

    ...

    private Actor[] actors = new Actor[1024];

    ...

    public Actor[] getActors(Class<?> cls) {
        //Count actors corresponding to class cls
        int cnt = 0;      
        for (int i = 0; i<actorsCount; i++)
           if (actors[i] instanceof cls) cnt++; 


        //Build a new array;
        Actor[] clsActors = new Actor[cnt];

        //Fill it
        for (int j = 0, k=0; j<cnt; k++)
           if (actors[k] instanceof cls)
              clsActors[j++] = actors[k];

        return clsActors;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误:" - 不兼容的操作数类型boolean和Class <capture#1-of?extends Scenario>"

"演员"由我的精灵扩展,例如Bird,Hero等.例如,想法是在给定时间获得场景中所有鸟类的列表以进行一些计算.

知道这里发生了什么吗?如何测试给定对象是否是给定类的实例?

java instanceof

20
推荐指数
3
解决办法
2万
查看次数

为什么instanceof不能使用Generic?

可能重复:
Java:Instanceof和Generics

我正在尝试编写一个将通用List转换为特定类型List的函数.找到下面的代码

public <T>List<T> castCollection(List srcList, Class<T> clas){
    List<T> list =new ArrayList<T>();
    for (Object obj : srcList) {
       if(obj instanceof T){
            ...
       }
    }
    return list;
}
Run Code Online (Sandbox Code Playgroud)

obj instanceof T显示编译错误 -

无法对类型参数T执行instanceof检查.请改为使用其擦除对象>,因为将在运行时擦除其他泛型类型信息.

任何澄清或获得预期结果的方法?

提前致谢.:)

java generics instanceof typeerror

20
推荐指数
2
解决办法
4万
查看次数

如何检查异常的类型以及它们的嵌套异常类型?

假设我捕获了一个类型异常AppException但我只想对该异常执行某些操作,如果它有类型的嵌套异常StreamException.

if (e instanceof AppException)
{
    // only handle exception if it contains a
    // nested exception of type 'StreamException'
Run Code Online (Sandbox Code Playgroud)

如何检查嵌套StreamException

java exception instanceof

19
推荐指数
2
解决办法
4万
查看次数

如何知道对象是否是TypeTag类型的实例?

我有一个函数,它能够知道一个对象是否是一个Manifest类型的实例.我想将它迁移到一个TypeTag版本.旧功能如下:

def myIsInstanceOf[T: Manifest](that: Any) = 
  implicitly[Manifest[T]].erasure.isInstance(that)
Run Code Online (Sandbox Code Playgroud)

我一直在试验TypeTags,现在我有了这个TypeTag版本:

// Involved definitions
def myInstanceToTpe[T: TypeTag](x: T) = typeOf[T]
def myIsInstanceOf[T: TypeTag, U: TypeTag](tag: TypeTag[T], that: U) = 
  myInstanceToTpe(that) stat_<:< tag.tpe

// Some invocation examples
class A
class B extends A
class C

myIsInstanceOf(typeTag[A], new A)        /* true */
myIsInstanceOf(typeTag[A], new B)        /* true */
myIsInstanceOf(typeTag[A], new C)        /* false */
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来完成这项任务?参数化是否U可以省略,使用Any替代(就像在旧函数中一样)?

scala instanceof scala-2.10

19
推荐指数
2
解决办法
7504
查看次数

Java中的instanceof - 为什么不编译?

class A {

    public static void main(String...args) {
        Integer var = 10; 

        if(var instanceof Character)  // Line1
            System.out.println("var is a Character");
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道第1行不会编译,因为编译器发现它var不是Character.

我不明白的是为什么编译器会引发错误,而不是返回的falsetrue.

如果编译器返回falsetrue(instanceof即将操作视为常规的基于if的验证),那么它会更有用..它不会吗?

还是我错过了一些明显的东西?

java instanceof

18
推荐指数
1
解决办法
2387
查看次数

目标c是KindOfClass的误解吗?

我有以下对象结构:

动物,狗和猫.正如你所料,狗和猫是从动物遗传的.

我有一个农场班:

 @implementation AnimalFarm

-(Animal*) createAnimal:(AnimalType)type{

  switch (type) {

    case CAT:
      return [Cat new];

    case DOG:
      return [Dog new];

    default:
      return [Animal new];
  }

}

@end
Run Code Online (Sandbox Code Playgroud)

我试着进行单元测试:

  AnimalFarm *farm = [AnimalFarm new];

  Animal *dog = [farm createAnimal:DOG];
  Animal *cat = [farm createAnimal:CAT];

  STAssertTrue([cat isMemberOfClass:[Cat class]],@"cat is not a cat!");
  STAssertTrue([dog isMemberOfClass:[Dog class]],@"Dog is not a dog!");

  STAssertTrue([cat isKindOfClass:[Animal class]],@"Cat is not an animal!");
  STAssertTrue([dog isKindOfClass:[Animal class]],@"Cat is not an animal!");
Run Code Online (Sandbox Code Playgroud)

课程实施:

@interface Cat : Animal {

}


@end

@implementation …
Run Code Online (Sandbox Code Playgroud)

objective-c instanceof

17
推荐指数
1
解决办法
4万
查看次数

检查注释是否属于特定类型

我使用反射来看看是否被连接到一个类的属性的注释,是一种特定类型的.目前我在做:

if("javax.validation.Valid".equals(annotation.annotationType().getName())) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

这让我觉得有点笨拙,因为它依赖于一个完全限定的类名字符串.如果命名空间将来发生更改,则可能会导致细微错误.

我想要做:

if(Class.forName(annotation.annotationType().getName()).isInstance(
     new javax.validation.Valid()
)) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

但它javax.validation.Valid是一个抽象类,无法实例化.有没有办法模拟instanceof(或基本上使用isInstance)对接口或抽象类?

java reflection annotations instanceof isinstance

17
推荐指数
4
解决办法
2万
查看次数

这个instanceof错误消息是什么意思?

我正在玩instanceofChrome,但收到了错误消息.我我知道为什么(你必须instanceof在创建对象的构造函数之后提供一个函数),但错误消息似乎在说明其他内容:

[1,2,3] instanceof Array
// true

[1,2,3] instanceof []
// TypeError: Expecting a function in instanceof check, but got 1,2,3
Run Code Online (Sandbox Code Playgroud)

这是否意味着我应该[1,2,3]用函数替换?我认为这[1,2,3]是正确的,这[]是问题,应该用一个函数替换,但看起来错误信息正好相反.

有人可以解释我是如何错误地解释错误消息的吗?

javascript instanceof

17
推荐指数
1
解决办法
2万
查看次数

避免Java中的instanceof

我在大学的某个阶段被告知(并且随后在十几个地方读过),使用instanceof应仅用作"最后的手段".考虑到这一点,是否有人能够告诉我以下代码是否是最后的手段.我已经看过堆栈溢出但是找不到类似的场景 - 也许我错过了它?

private void allocateUITweenManager() {
   for(GameObject go:mGameObjects){
      if (go instanceof GameGroup) ((GameGroup) go).setUITweenManager(mUITweenManager);
   }
}
Run Code Online (Sandbox Code Playgroud)

哪里

  • mGameObjects是一个数组,只有一些是GameGroup类型
  • GameGroup是抽象类的子类GameObject.
  • GameGroup使用UITweenable具有方法的接口setUITweenManager()
  • GameObject 不使用界面 UITweenable

我想我可以同样(也可能应该)替换GameGroup上面的代码UITweenable- 我会问同样的问题.

还有另一种方法可以避免这种情况instanceof吗?这段代码不能失败,因此(我认为,对吧?),但鉴于坏消息instanceof似乎得到了,我是否已经在我instanceof在这里使用的线路上某处犯了一些OOP的主要罪?

提前致谢!

java oop inheritance instanceof

17
推荐指数
2
解决办法
3463
查看次数