Long l1 = null;
Long l2 = Long.getLong("23");
Long l3 = Long.valueOf(23);
System.out.println(l1 instanceof Long); // returns false
System.out.println(l2 instanceof Long); // returns false
System.out.println(l3 instanceof Long); // returns true
Run Code Online (Sandbox Code Playgroud)
我无法理解返回的输出.我期待第二和第三个系统的真正至少.有人能解释一下instanceof的工作原理吗?
您好,我想知道有什么比这更优雅的替代品:
class Base...
class A extends Base...
class B extends Base...
//iterator of colection containing mixed As and Bs i want to remowe Bs and do omething with As
while(iterator.hasNext()) {
Base next = iterator.next();
if(next instanceof A) // do something
if(next instanceof B)
iterator.remove();
}
Run Code Online (Sandbox Code Playgroud)
播种替代品...
谢谢你的建议。
编辑:基类可能有许多子类,而不仅仅是两个,它们的数量可能会随着时间增长
我有一些测试课
TestKlass = (function() {
function TestKlass(value) {
this.value = value != null ? value : 'hello world';
console.log(this.value);
}
return TestKlass;
})();
x = new TestKlass;
x instanceof TestKlass; (gives true)
Run Code Online (Sandbox Code Playgroud)
我有新的空物
y = {}
y instanceof Object
Run Code Online (Sandbox Code Playgroud)
我能找到任何方法来为y设置任何属性,就像这样
y.__proto__ = x.__proto__
y.constructor.prototype = x.constructor.prototype
Run Code Online (Sandbox Code Playgroud)
为了得到这个结果
y instanceof TestKlass => true
Run Code Online (Sandbox Code Playgroud)
================================================== ==
UPD:
所以.我的主要目标 - 创建CLONE功能.现在我的解决方案适合我.请看这段代码:
JavaScript JS对象克隆
Object._clone = function(obj) {
var clone, property, value;
if (!obj || typeof obj !== 'object') {
return obj; …Run Code Online (Sandbox Code Playgroud) instanceof如何正常工作?当我有一个扩展并实现彼此的对象层次结构时,某些事物的实例是否通过这两条线工作?
例如,我想知道如果我的对象是实例List或ArrayList或Collection?
我检查了这棵树,http://docs.oracle.com/javase/6/docs/api/java/util/package-tree.html
而且他们似乎完全属于Object当下,但我认为我需要的是AbstractCollection,甚至是正常的Collection,因为这似乎是层次结构中最高的.
当我检查一个对象只Collection覆盖所有这三个类时,我会没事吗?
编译代码时,我收到了关于Object Casting的警告消息.我不知道如何使用我目前的知识修复它....假设我有一个通用对象MyGenericObj<T>它是从非通用对象扩展的MyObj
这是一个示例代码:
MyObj obj1 = new MyGenericObj<Integer>();
if (obj1 instanceof MyGenericObj) {
//I was trying to check if it's instance of MyGenericObj<Integer>
//but my IDE saying this is wrong syntax....
MyGenericObj<Integer> obj2 = (MyGenericObj<Integer>) obj1;
//This line of code will cause a warning message when compiling
}
Run Code Online (Sandbox Code Playgroud)
能不能让我知道这样做的正确方法是什么?
任何帮助表示赞赏.
当我们的对象层次结构纯粹是语义的继承而不是行为的继承时,那么我们不可避免地需要在任何地方编写"instanceof"或"if/else"来进行运行时类型检查.
例如
如果我有一个对象层次结构
Class Function
Class Average extends Function
Class Sum extends Function
Class Max extends Function
Run Code Online (Sandbox Code Playgroud)
如果在这些类中有一个名为calculate()的方法,那么我们没有问题,我们可以利用多态性,这种设计满足LSP.
但是,如果由于某种原因我们不想将此calculate()方法添加到此层次结构中,则这些对象纯粹是对象无状态对象,只表示语义.
然后我们被迫在任何地方编写以下代码:
if (function instanceof Average)
//perform average
else if(function instanceof Sum)
//perform sum
else if(function instanceof Max)
//perform max
Run Code Online (Sandbox Code Playgroud)
上面的代码表明设计不好,因为你在任何地方编写这个代码,这个设计很脆弱,以后很难改变.我想如果数字函数是有限的并且函数的计算在一个地方,这可能是好的取决于复杂性.
到目前为止我所知道的是,要解决上述方法,唯一可行的方法是实现访问者模式,除了使用访问者模式之外,还有其他方法可以解决上述设计吗?
我可以从访问者模式看到的一个问题是访问者模式的accept方法没有返回值,如果accept()方法不能完全满足需求,这有时候不方便.
java design-patterns liskov-substitution-principle instanceof visitor-pattern
我是Haskell的新手,所以我的问题可能很愚蠢.
我想要一个功能
show2 :: (Show a) => a -> String
Run Code Online (Sandbox Code Playgroud)
这将返回show a任何a,但a如果一个本身String.我该如何实现它?
如果这个功能已经在某个地方实现了,那就太好了,但是我仍然希望看到一个实现的例子.
该文章如下定义的instanceof:
instanceof运算符测试对象在其原型链中是否具有构造函数的prototype属性.
这是一个公平的解释,生活很好,直到我从Eloquent Javascript这本书中看到这个代码:
function TextCell(text) {
this.text = text.split("\n");
}
TextCell.prototype.minWidth = function() {
return this.text.reduce(function(width, line) {
return Math.max(width, line.length);
}, 0);
}
TextCell.prototype.minHeight = function() {
return this.text.length;
}
TextCell.prototype.draw = function(width, height) {
var result = [];
for (var i = 0; i < height; i++) {
var line = this.text[i] || "";
result.push(line + repeat(" ", width - line.length));
}
return result;
}
function RTextCell(text) {
TextCell.call(this, text);
}
RTextCell.prototype = Object.create(TextCell.prototype);
RTextCell.prototype.draw …Run Code Online (Sandbox Code Playgroud)Parameter[] ps = method.getParameters();
Map<String,Integer> map = new HashMap<String,Integer>();
for(int ij = 0;ij<ps.length;ij++){
Parameter p = ps[ij];
RequestParam rp = p.getAnnotation(RequestParam.class);
if(rp != null){
//do something
}else {
System.out.println(p.getType());
System.out.println(p.getType().isInstance(HttpServletRequest.class));
System.out.println(p.getType() == HttpServletRequest.class);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
interface javax.servlet.http.HttpServletRequest
false
true
Run Code Online (Sandbox Code Playgroud)
为什么使用"isInstance"为false并使用"=="为真?因为"实例"无法判断实现关系?
instanceof ×10
java ×7
javascript ×2
casting ×1
ecmascript-5 ×1
haskell ×1
inheritance ×1
jvm ×1
liskov-substitution-principle ×1
object ×1
warnings ×1