1)、instanceof 在Java中的应用:
instanceof是Java的一个二元操作符,和==,>,<是同一类东东。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据,指出对象是否是特定类的一个实例。 如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。 如果 object 不是指定类的一个实例,或者 object 是null,则返回 false。
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
Object src = event.getSource();
if (src != null && src instanceof Component) {
Component component = (Component) src;
Window window = null;
if (component instanceof Window) {
window = (Window) component;
} else {
window = SwingUtilities.getWindowAncestor(component);
}
if (window != null && !windows.contains(window)) {
if (window instanceof RootPaneContainer) {
addMouseListener(((RootPaneContainer) window)
.getContentPane());
} else {
addMouseListener(window);
}
windows.add(window);
}
}
}
2 )、instanceof在 JavaScript 中的应用:
用法
object instanceof class
参数
object 动作脚本对象。
class 对动作脚本构造函数(例如 String 或 Date)的引用。
返回
如果 object 是 class 的实例,则 instanceof 返回 true;否则,instanceof 返回 false。
说明
运算符;确定某对象是否属于指定的类。测试 object 是否为 class 的实例。
如果该构造函数的原型对象位于此动作脚本对象的原型链中,则此动作脚本对象是该类的实例。
instanceof 运算符不会将原始类型转换为包装对象。例如,下面的代码返回 true:
new String("Hello") instanceof String
而下面的代码返回 false:
"Hello" instanceof String
示例:
function instanceof (theObject, theClass){
while ((theObject = theObject.__proto__) != null) {
if (theObject == theClass.prototype) {
return true;
}
}
return false;
}
3) .instanceof 在 .net 中的应用:
Public NotInheritable Class Instanceof _ Inherits BinaryOp
Dim instance As Instanceof
public sealed class Instanceof : BinaryOp
public ref class Instanceof sealed : public BinaryOp
public final class Instanceof extends BinaryOp-----------------详细情况请参照MSDN中的讲解,在MSDN中关于instanceof讲解的比较详细。
本文详细介绍了instanceof关键字在Java、JavaScript及.NET中的使用方法。在Java中,instanceof用于判断对象是否为某个类的实例;在JavaScript中,instanceof则用于判断对象是否属于指定的类;而在.NET中,instanceof的用法与Java类似。
133

被折叠的 条评论
为什么被折叠?



