什么是instanceof用于操作?我见过像这样的东西
if (source instanceof Button) {
//...
} else {
//...
}
Run Code Online (Sandbox Code Playgroud)
但这对我来说都没有意义.我已完成了我的研究,但仅提供了没有任何解释的例子.
反正有没有检测JavaScript对象是否是正则表达式?
例如,我想做这样的事情:
var t = /^foo(bar)?$/i;
alert(typeof t); //I want this to return "regexp"
Run Code Online (Sandbox Code Playgroud)
这可能吗?
谢谢!
编辑:谢谢你的所有答案.看来我有两个非常好的选择:
obj.constructor.name === "RegExp"
Run Code Online (Sandbox Code Playgroud)
要么
obj instanceof RegExp
Run Code Online (Sandbox Code Playgroud)
这两种方法的主要优点/缺点是什么?
再次感谢!
在我查看值的索引的通用数据结构之前,我想看看它是否this已经参数化的类型的实例.
但是当我这样做时Eclipse会抱怨:
@Override
public int indexOf(Object arg0) {
if (!(arg0 instanceof E)) {
return -1;
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
无法对类型参数E执行instanceof检查.请改为使用其擦除对象,因为泛型类型信息将在运行时被擦除
有什么更好的方法呢?
如何测试if a是否为子类b?
Class<?> a = A.class;
Class<?> b = B.class;
Run Code Online (Sandbox Code Playgroud) 当使用getClass()和==运算符over instanceOf运算符时,我看到了性能的提升.
Object str = new Integer("2000");
long starttime = System.nanoTime();
if(str instanceof String) {
System.out.println("its string");
} else {
if (str instanceof Integer) {
System.out.println("its integer");
}
}
System.out.println((System.nanoTime()-starttime));
starttime = System.nanoTime();
if(str.getClass() == String.class) {
System.out.println("its string in equals");
} else {
if(str.getClass() == Integer.class) {
System.out.println("its integer");
}
}
System.out.println((System.nanoTime()-starttime));
Run Code Online (Sandbox Code Playgroud)
是否有任何准则,哪一个使用getClass()或instanceOf?
给定一个情景:我知道要匹配精确类,即String,Integer(这些都是final类)等.
使用instanceOf运算符不好的做法?
具有一系列"instanceof"操作被认为是"代码味道".标准答案是"使用多态".在这种情况下我该怎么做?
基类有许多子类; 没有一个在我的控制之下.类似的情况是Java类Integer,Double,BigDecimal等.
if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);}
else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);}
else if (obj instanceof Double) {DoubleStuff.handle((Double)obj);}
Run Code Online (Sandbox Code Playgroud)
我确实可以控制NumberStuff等等.
我不想在几行代码中使用多行代码.(有时我将一个HashMap映射到一个IntegerStuff的实例,将BigDecimal.class映射到一个BigDecimalStuff的实例等等.但是今天我想要一些更简单的东西.)
我想要像这样简单的东西:
public static handle(Integer num) { ... }
public static handle(BigDecimal num) { ... }
Run Code Online (Sandbox Code Playgroud)
但是Java不会那样工作.
我想在格式化时使用静态方法.我正在格式化的东西是复合的,其中Thing1可以包含一个数组Thing2s和Thing2可以包含一个Thing1s数组.当我实现这样的格式化程序时,我遇到了问题:
class Thing1Formatter {
private static Thing2Formatter thing2Formatter = new Thing2Formatter();
public format(Thing thing) {
thing2Formatter.format(thing.innerThing2);
}
}
class Thing2Formatter {
private static Thing1Formatter thing1Formatter = new Thing1Formatter();
public format(Thing2 thing) {
thing1Formatter.format(thing.innerThing1);
}
}
Run Code Online (Sandbox Code Playgroud)
是的,我知道HashMap和更多代码也可以修复它.但相比之下,"instanceof"似乎更具可读性和可维护性.有什么简单但不臭吗?
注释已添加5/10/2010:
事实证明,将来可能会添加新的子类,而我现有的代码必须优雅地处理它们.在这种情况下,类上的HashMap不起作用,因为找不到类.一系列if语句,从最具体的开始到以最一般的结尾,可能是最好的:
if (obj instanceof SubClass1) …Run Code Online (Sandbox Code Playgroud) java reflection polymorphism instanceof chain-of-responsibility
如果Object是不使用反射的数组,我如何在Java中看到?如何在不使用反射的情况下迭代所有项目?
我使用谷歌GWT所以我不允许使用反射:(
我想在不使用refelection的情况下实现以下方法:
private boolean isArray(final Object obj) {
//??..
}
private String toString(final Object arrayObject) {
//??..
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句:我也不想使用JavaScript,以便我可以在非GWT环境中使用它.
我想知道instanceofJava 中运算符的以下行为.
interface C {}
class B {}
public class A {
public static void main(String args[]) {
B obj = new B();
System.out.println(obj instanceof A); //Gives compiler error
System.out.println(obj instanceof C); //Gives false as output
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?interface C和之间没有关系class B,但是它给出了假,而在obj instanceof A它出现编译错误的情况下?
我知道is和asfor instanceof,但是反射isInstance()方法怎么样?
我想检查对象o是否是类的实例C或子类的实例C.
举例来说,如果p是类的Point我想x.instanceOf(Point.class)是true也x.instanceOf(Object.class)为true.
我希望它也适用于盒装原始类型.例如,如果x是,Integer那么x.instanceOf(Integer.class)应该是true.
有这样的事吗?如果没有,我该如何实现这样的方法?
instanceof ×10
java ×8
reflection ×3
class ×2
arrays ×1
c# ×1
constructor ×1
generics ×1
gwt ×1
inheritance ×1
interface ×1
javascript ×1
polymorphism ×1
regex ×1
subclass ×1
typechecking ×1
typeof ×1