- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一组具有公共(public)父类(super class)的 POJO。这些存储在 superclass 类型的二维数组中。现在,我想从数组中获取一个对象并使用子类 的方法。这意味着我必须将它们转换为子类。有没有办法不使用 instanceof 来做到这一点?
更新:作为一个具体的例子:http://obviam.net/index.php/the-mvc-pattern-tutorial-building-games/请参阅:“单击敌人时添加新 Action (攻击)”
最佳答案
是的 - 你可以通过反转流程来做到这一点:当基类的实例是特定类型时,而不是你的代码做某事,传递一个 Action 项给对象,让对象决定是否执行它或不。这是 Visitor Pattern 背后的基本技巧.
interface DoSomething {
void act();
}
abstract class AbstractBaseClass {
abstract void performAction(DoSomething ds);
}
class FirstSubclass extends AbstractBaseClass {
public void performAction(DoSomething ds) {
ds.act();
}
}
class SecondSubclass extends AbstractBaseClass {
public void performAction(DoSomething ds) {
// Do nothing
}
}
AbstractBaseClass array[] = new AbstractBaseClass[] {
new FirstSubclass()
, new FirstSubclass()
, new SecondSubclass()
, new FirstSubclass()
, new SecondSubclass()
};
for (AbstractBaseClass b : array) {
b.performAction(new DoSomething() {
public void act() {
System.out.println("Hello, I'm here!");
}
});
}
关于java - 避免 instanceof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11415090/
对于以下条件: if (a != null && a instanceof A) 或 if (a instanceof A) 首先检查 null 是否有任何优势(例如,性能方面)?两个条件的结果应该相
我正在编写一个方法,如果异常是 *someClass* 的实例,它将返回 true。但是我无法导入其中一个类,因为它在另一个模块中。我无法将该模块添加到项目中,所以我想,我不能使用 instanceo
这个问题在这里已经有了答案: The difference between "instanceof List" and 'o instanceof List" (2 个答案) 关闭 7 年前。 我知
多年来,我尽量避免使用 instanceof。在适用的情况下使用多态性或访问者模式。我想它只是在某些情况下简化了维护......还有其他需要注意的缺点吗? 但是我确实在 Java 库中到处看到它,所以
我没有看到以下任何区别: Object o = new LinkedList(); System.out.println(o instanceof List); System.
我正在像这样扩展对象: Object.prototype.is_a = function (x) { return this instanceof x; } 一切正常 "foo".is_a(Str
我在 NetBeans 中编码如下: public class Grafo { class Par { int a, b; Par(int a, int
我已经动态创建了布局。它有一些编辑文本、 TextView 、微调器等。 之后,我必须获取我介绍的关于这些 View 的信息。 所以我在做这样的事情 for(int i=0;i <= childs;i
等等!我知道还有其他非常相似的问题,但(也许是我)我需要回答其中的特定部分。 我知道可以说 Object.prototype 位于委托(delegate)链的最顶端。但是,在 Function 存在以
MooTools 有自己的instanceOf(instance, Type) 函数。 我只能假设它做了一些与 Javascript 的原生 instanceof 运算符不同的事情,但我似乎无法弄清楚
获取UTXO的方法 getUtoxs(address){ var options; if(Global.btc.network === "testnet"){ options = {
为什么在 JavaScript 中,Object instanceof Function 和 Function instanceof Object 都返回 true? 我在 Safari WebIns
为什么在 JavaScript 中 Object instanceof Function 和 Function instanceof Object 都返回 true? 我在 Safari WebIns
首先,我制作了一个“游戏渲染器”。 我的问题是,当我需要绘制当前元素时:我需要知道它是矩形、圆形还是图像等。 我的类(矩形、圆形...)是从图形扩展而来的。 public class Rectangl
想象一下,我想编写一个名为 isInstanceof 的无用方法,它返回一个 boolean。 我在想。但我不出去。 instanceof 必须像这样使用: [object] instanceof [
我有两个继承自 Player 的类:SimplePlayer 和 InterleavedPlayer。 B 类有这些方法:setPlayer(SimplePlayer player) 和 setPla
我在尝试使用以下内容时遇到了意外行为: $object instanceof $class 1/如 in the official doc. 所述,PHP 'instanceof' 关键字和命名空间可
可以在 angularjs 中使用 typeof 吗? 我有一个 ngrepeat 循环遍历我的数据,应该检查数据是字符串还是对象。 {{angular.isObject(text) &&
例如,我有 Currency 和处理 Currency 各种实现的 Exchange。我想对窗帘货币有额外的汇率,我这样做 interface Currency { double rate();} i
我有以下代码: abstract class BaseToken {} class OpenParen extends BaseToken { public static assert(t:
我是一名优秀的程序员,十分优秀!