请考虑以下代码.
function a() {}
function b() {}
function c() {}
b.prototype = new a();
c.prototype = new b();
console.log((new a()).constructor); //a()
console.log((new b()).constructor); //a()
console.log((new c()).constructor); //a()
Run Code Online (Sandbox Code Playgroud)
此外,请考虑以下内容.
console.log(new a() instanceof a); //true
console.log(new b() instanceof b); //true
console.log(new c() instanceof c); //true
Run Code Online (Sandbox Code Playgroud)
(new c()).constructor等于a()和Object.getPrototypeOf(new c())是a{ },怎么可能instanceof知道这new c()是一个实例c?做的用例是什么new String("already a string")?
它的重点是什么?
有没有办法instanceof在EL 进行检查?
例如
<h:link rendered="#{model instanceof ClassA}">
#{errorMessage1}
</h:link>
<h:link rendered="#{model instanceof ClassB}">
#{errorMessage2}
</h:link>
Run Code Online (Sandbox Code Playgroud) 有没有办法找到泛型的类型?
if (T instanceof String) {
// do something...
}
Run Code Online (Sandbox Code Playgroud)
以上绝对不会编译.
在JavaScript中,我可以通过以下方式声明字符串;
var a = "Hello World";
var b = new String("Hello World");
Run Code Online (Sandbox Code Playgroud)
但是a不是String的实例......
console.log(a instanceof String); //false;
console.log(b instanceof String); //true;
Run Code Online (Sandbox Code Playgroud)
那么如何找到类型或" instanceof"字符串文字?
可以强制JavaScript new String()为每个字符串文字创建一个?
我正在设计一款游戏.在游戏中,各种游戏对象根据他们需要做的事情扩展不同的接口(和一个抽象类),并传递给处理程序,处理程序按照定义的时间间隔处理具有特定接口的项目(它们实际上将所有工作分散开来以一种简洁的方式确保始终处理输入/视频/等).
无论如何,其中一些对象扩展了抽象类Collider并传递给CollisionHandler.Collider类和处理程序负责碰撞中涉及的所有技术,并且只要求对象实现collidesWith(Collider c)函数,并根据它碰撞的内容进行修改.
许多不同类的对象将彼此碰撞,并且将根据它们碰撞的对象的类型及其特定属性而以非常不同的方式起作用.
完美的解决方案似乎是像这样使用instanceof:
class SomeNPC extends Collider{
collidesWith(Collider c){
if(c instanceof enemy){
Fight it or run away depending on your attributes and theirs.
}
else if(c instanceof food){
Eat it, but only if it's yellow.
}
else if(c instanceof BeamOfLight){
Try to move towards its source.
}
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上似乎是一个合法的地方.我只是感觉不舒服.就像一个goto在某些特定情况下有意义一样.设计是否对任何人都有根本感觉?如果是这样,你会建议做什么来实现相同的行为.
在我的一个项目中,我有两个"数据传输对象"RecordType1和RecordType2,它们继承自RecordType的抽象类.
我希望两个RecordType对象在"process"方法中由同一个RecordProcessor类处理.我的第一个想法是创建一个通用的流程方法,该方法委托给两个特定的流程方法,如下所示:
public RecordType process(RecordType record){
if (record instanceof RecordType1)
return process((RecordType1) record);
else if (record instanceof RecordType2)
return process((RecordType2) record);
throw new IllegalArgumentException(record);
}
public RecordType1 process(RecordType1 record){
// Specific processing for Record Type 1
}
public RecordType2 process(RecordType2 record){
// Specific processing for Record Type 2
}
Run Code Online (Sandbox Code Playgroud)
我读过Scott Meyers在Effective C++中写了以下内容:
"任何时候你发现你自己编写的形式的代码'如果对象是T1类型,那么做一些事情,但如果它是T2类型,那么做一些其他的事情,'打自己."
如果他是对的,显然我应该打自己.我真的没有看到这是多么糟糕的设计(除非当然有人将RecordType子类化并添加到RecordType3而不向处理它的通用"Process"方法添加另一行,从而创建一个NPE),以及我能想到的替代方案涉及将特定处理逻辑首当其冲地放在RecordType类本身中,这对我来说真的没有多大意义,因为理论上我可以对这些记录执行许多不同类型的处理.
有人可以解释为什么这可能被视为糟糕的设计并提供某种替代方案,仍然负责将这些记录处理到"处理"类?
更新:
return null以throw new IllegalArgumentException(record);我正在使用node.js,所以这可能是V8特有的.
我总是注意到typeof和instanceof之间存在一些奇怪的差异,但这里有一个让我烦恼的事:
var foo = 'foo';
console.log(typeof foo);
Output: "string"
console.log(foo instanceof String);
Output: false
Run Code Online (Sandbox Code Playgroud)
那里发生了什么?
在开始之前,我知道这个问题有很多答案可以提出替代方法.我正在寻求这种特殊方法的帮助,以确定是否可行,如果不可能,可能会有类似的方法.
我有一个方法,它接受一个超类并根据传递的对象的类型调用一个方法.例如:
public void handle(Object o){
if (o instanceof A)
handleA((A)o);
else if (o instanceof B)
handleB((B)o);
else if (o instanceof C)
handleC((C)o);
else
handleUnknown(o);
Run Code Online (Sandbox Code Playgroud)
我不能修改子类型来覆盖一个handle()方法,正如这个答案所暗示的那样,因为我不拥有这些类.所以这个instanceof方法就是我所拥有的.
我想用一个switch声明代替if/else,只是因为它更整洁.我知道你只能打开基元和字符串,所以我要切换类名:
switch(o.getClass().getCanonicalName()){
case "my.package.A":
handleA((A)o);
break;
case "my.package.B":
handleB((B)o);
break;
case "my.package.C":
handleC((C)o);
break;
default:
handleUnknown(o);
break;
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是规范名称非常长(如12个子包),我不能调用ClassName.class.getCanonicalName()case语句,因为Java不允许这样做.所以我的下一个解决方案是Enum.这是我遇到问题的地方.
我希望我的代码看起来像这样:
public enum Classes {
A (A.getCanonicalName()),
B (B.getCanonicalName()),
C (C.getCanonicalName());
}
switch (o.getClass().getCanonicalName()){
case Classes.A:
handleA((A)o);
break;
case Classes.B:
handleB((B)o);
break;
case …Run Code Online (Sandbox Code Playgroud) instanceof ×10
java ×4
javascript ×4
string ×2
typeof ×2
types ×2
class ×1
constructor ×1
el ×1
enums ×1
generics ×1
inheritance ×1
jsf ×1
node.js ×1
php ×1
polymorphism ×1
syntax ×1
traits ×1