如何在Java中看到Object是否是不使用reflection的数组? 我怎样才能不使用reflection迭代所有项目? 我使用谷歌GWT,所以我不允许使用reflection:( 我很乐意实施下列方法而不使用反思: private boolean isArray(final Object obj) { //??.. } private String toString(final Object arrayObject) { //??.. } 顺便说一句:我也不想使用JavaScript,以便我可以在非GWT环境中使用它。
我有以下(也许是常见的)问题,目前绝对困惑我: 有几个生成的事件对象扩展了抽象类Event ,我想把它们分成Session Beans,就像 public void divideEvent(Event event) { if (event instanceof DocumentEvent) { documentGenerator.gerenateDocument(event); } else if (event instanceof MailEvent) { deliveryManager.deliverMail(event); … } … } 但是将来可能会有两种以上的事件types,所以if-else会很长,也许不可读。 另外我认为instanceof在这种情况下并不是真正的“最佳实践”。 我可以将一个抽象方法添加到Eventtypes,并让它们自己分割,但是我必须在每个实体中注入特定的Session Beans。 是否有任何暗示为这个问题实现“漂亮”的解决scheme? 谢谢你的帮助!
为什么这个代码不能编译? public boolean isOf(Class clazz, Object obj){ if(obj instanceof clazz){ return true; }else{ return false; } } 为什么我不能将类variables传递给instanceof ?
我想检查一个对象o是类C还是C的一个子类的一个实例。 例如,如果p是类Point我希望x.instanceOf(Point.class)为true ,并且x.instanceOf(Object.class)为true 。 我希望它也适用于盒装原始types。 例如,如果x是一个Integer那么x.instanceOf(Integer.class)应该是true 。 有这样的事吗? 如果没有,我该如何实施这样的方法?
使用getClass()和==运算符优于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)); 有没有任何指南,哪一个使用getClass()或instanceOf ? 给定一个场景:我知道确切的类匹配,即String […]
我知道is和为instanceof ,但reflectionisInstance()方法呢?
我想知道关于Java中instanceof操作符的下面的行为。 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 } } 为什么这样? interface C和class B之间没有关系,但它给出了错误,而在obj instanceof A情况下,它给编译器错误?
有一个“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);} 我确实可以控制NumberStuff等等。 我不想在几行代码中使用多行代码。 (有时我做一个HashMap映射Integer.class到一个IntegerStuff的实例,BigDecimal.class到一个BigDecimalStuff的实例等等。但是今天我想要更简单一些。) 我想要这样简单的事情: public static handle(Integer num) { … } public static handle(BigDecimal num) { … } 但是Java不能这样工作。 我想在格式化时使用静态方法。 我格式化的东西是复合的,其中Thing1可以包含一个Thing2s数组,Thing2可以包含Thing1s数组。 当我执行这样的格式化程序时遇到问题: class Thing1Formatter { private static Thing2Formatter thing2Formatter = new Thing2Formatter(); […]
无论如何检测一个JavaScript对象是否是一个正则expression式? 例如,我想要做这样的事情: var t = /^foo(bar)?$/i; alert(typeof t); //I want this to return "regexp" 这可能吗? 谢谢! 编辑:感谢所有的答案。 看来我有两个很好的select: obj.constructor.name === "RegExp" 要么 obj instanceof RegExp 任何方法的主要优点/缺点? 再次感谢!
这真的只是为了满足我的好奇心的一个基本问题,但有没有办法做到这样的事情: if(obj !instanceof Array) { //The object is not an instance of Array } else { //The object is an instance of Array } 这里的关键是能够使用NOT! 在实例前面。 通常我必须设置的方式是这样的: if(obj instanceof Array) { //Do nothing here } else { //The object is not an instance of Array //Perform actions! } 当我只想知道对象是否是特定types时,有一点烦人的是必须创build一个else语句。