标签: instanceof

我真的应该像瘟疫一样避免这种情况吗?

我正在开发一种棋盘游戏,包括将棋子放入牢房.只要在那里没有任何对手的棋子,你就可以把棋子放在棋盘上的任何地方(包括被占用的牢房).

现在我已经考虑过两种替代方法:

  • 没有instanceof:

RedPiece.class片段

public boolean isAlly(RedPiece p) {
    return true;
}

public boolean isAlly(BluePiece p) {
    return false;
}

public boolean isAlly(GreenPiece p) {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

显然,如果我想添加一种新颜色,我必须向每个类添加另一种方法.

  • 使用instanceof:

Piece.class片段

public boolean isAlly(Piece that) { 
    return this instanceof that;
}
Run Code Online (Sandbox Code Playgroud)

后来这种方法对我来说似乎更好,因为在添加新颜色时我不需要做任何事情.

当然对于我的游戏来说这不是问题,因为所有部分都是相同的,我可以只创建一个枚举而不是新的类.但是,如果红队突然被赋予蓝色和绿色没有的特殊能力呢?

也许解决方案是添加一个字符串属性,说明他们属于哪个团队(红色玩家和蓝色玩家),但现在它的设计使得红色部分只属于红队.字符串"RED"绝对是您可以从课程中获得的冗余信息.

总结:在这里使用instanceof是否合理?谁能想到更好的方法?

java instanceof

0
推荐指数
1
解决办法
141
查看次数

存储其他数据类型的数据类型是什么?

注意:我不是在询问可以使用哪些类型来存储类名,也不是要求使用解决方法来存储类名,例如反射或使用Strings.这也不是询问如何使用instanceof运算符的问题.

考虑这个例子:

A a = new A();
Class c1 = A.class;
System.out.println(a instanceof c1);    //Does not Work

Class c2 = A.class.getClass();
System.out.println(a instanceof c2);    //Does not Work

class A{}
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码会失败.但我很好奇是否可以存储一个类,以便它可以像我正在键入类名本身一样工作.

System.out.println(a instanceof A);    //This will work
Run Code Online (Sandbox Code Playgroud)

我们可以保留(类)A一些变量,xx以便这可以工作吗?

System.out.println(a instanceof xx);   
Run Code Online (Sandbox Code Playgroud)

我的问题是:是否可以存储A一个变量,以便编译器将该变量看作是看到它A

注意:我不是在问如何存储A的对象.我在问是否可以存储A本身.

java variables class instanceof

0
推荐指数
1
解决办法
46
查看次数

Javascript instanceof奇怪的行为

为什么它会返回false?

function f(){ return f; }
new f() instanceof f; // Prints false instead of true 
Run Code Online (Sandbox Code Playgroud)

据我了解,在这种特殊情况下instanceof应该检查如下:

newObj.__proto__ === f.prototype.

并且newObj.__proto__应该在new f()通话时自动设置.

javascript prototype instanceof

0
推荐指数
1
解决办法
83
查看次数

在原始类型上使用instanceof方法的Java会产生编译错误

我正在学习Java,我不明白为什么下面的代码没有编译而没有错误:

public class SecondClass{

public static void main(String[] args){
    int number = 45;
    if (number instanceof String) {
        System.out.println("Not a String!");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么我在条件操作中出错?本instanceof应该返回truefalse向右?在这种情况下,应该有false,因为number是一个int,但是这个代码不编译.

java instanceof

0
推荐指数
1
解决办法
291
查看次数

什么更快:instanceof或isInstance?

除了设计问题,在现代JVM上执行速度更快的是什么?

foo instanceof Bar
Run Code Online (Sandbox Code Playgroud)

要么

Bar.class.isInstance(foo)
Run Code Online (Sandbox Code Playgroud)

为什么?

java jvm instanceof

0
推荐指数
1
解决办法
757
查看次数

Java不会认识到该对象属于子类

所以我正在做一些关于制作一个小游戏原型的课程.我有这些简单的类(以及其他一些不相关的类):

abstract class Weapon {
    int damage;
    int cost; 
}

abstract class RangedWeapon extends Weapon {
    int range;
    int rounds;
}

class ExtraRounds extends Item{
    int cost = 20;
    int uses = 1;
    void use(GameState state){
        if (state.currentCharacter.weapon instanceof RangedWeapon){
            state.currentCharacter.weapon.rounds += 10;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但是在尝试编译时,我得到了

Implementations.java:56: error: cannot find symbol
            state.currentCharacter.weapon.rounds += 10;
                                         ^
  symbol:   variable rounds
  location: variable weapon of type Weapon
Run Code Online (Sandbox Code Playgroud)

我想要的只是班级ExtraRounds来检查是否weapon是班级RangedWeapon并采取相应的行动,但我不知道哪里出了问题.任何帮助表示赞赏

java inheritance abstract-class instanceof

0
推荐指数
1
解决办法
56
查看次数

计算每个类型的对象数 - Instanceof或getClassName

我有三个类- ,,OneTwo extends OneThree extends Two

我必须编写一个方法来计算每个类中存在多少个实例ArrayList<One>.

ArrayList<One> v = new ArrayList<>(3);
    v.add(new One();
    v.add(new Two();
    v.add(new Three();
Run Code Online (Sandbox Code Playgroud)

工作代码:

public static void test2(ArrayList<One> v){
    String className = "";
    int countOne = 0, countTwo = 0, countThree = 0;
    for (int i = 0; i <v.size() ; i++) {
        className = v.get(i).getClass().getSimpleName();
        if (className.equals("One")){
            countOne++;
        }
        else if (className.equals("Two")){
            countTwo++;
        }
        else{
            countThree++;
        }

    }
    System.out.println("One = "+countOne + "Two = " + countTwo + "Three …
Run Code Online (Sandbox Code Playgroud)

java instanceof

0
推荐指数
1
解决办法
54
查看次数

需要有关 Instanceof 运算符中类型转换的信息

当我将字符串转换为对象时,以下instanceof运算符工作正常。

但是,如果没有类型转换,它就无法编译。

public class Test {
        public static void main(String[] args) {
            boolean b1 = "abc" instanceof Integer; // Compilation fails
            boolean b2 = ((Object) "abc") instanceof Integer; // Works fine
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么编译器拒绝第一个b1但允许第二个b2

java instanceof

0
推荐指数
1
解决办法
158
查看次数

将instanceof与Object类一起使用

使这项工作的正确语法是什么?

public boolean isTypeOf(Class type) {
     return this instanceof type;
}
Run Code Online (Sandbox Code Playgroud)

我打算用它来称呼它:

foo.isTypeOf(MyClass.class);
Run Code Online (Sandbox Code Playgroud)

该方法将被覆盖,否则我将使用instanceofinplace.

java methods class instanceof

-1
推荐指数
1
解决办法
1077
查看次数

语法错误的实例?

我有一个与" instanceof" 相关的练习,我不太清楚如何使用它.这就是我想出的:

for(int i = 4; i < 6; i++){
    int availSupply = part[i].stockLevel+part[i].getAvailForAssembly();
    if(availSupply instanceof Integer){
        System.out.println("Total number of items that can be supplied for "+ part[i].getID()+"(" + part[i].getName() + ": "+ availSupply);
    }
}
Run Code Online (Sandbox Code Playgroud)

代码看起来很好,但是它出现了一个错误:

Multiple markers at this line

Incompatible conditional operand types int and Integer at:  if(availSupply instanceof Integer){
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么,这是唯一出现的错误.

java instanceof

-1
推荐指数
1
解决办法
667
查看次数