instanceof

instanceof check in EL expression language

删除回忆录丶 提交于 2019-12-17 10:47:27
问题 Is there a way to perform an instanceof check in EL? E.g. <h:link rendered="#{model instanceof ClassA}"> #{errorMessage1} </h:link> <h:link rendered="#{model instanceof ClassB}"> #{errorMessage2} </h:link> 回答1: You could compare Class#getName() or, maybe better, Class#getSimpleName() to a String . <h:link rendered="#{model['class'].simpleName eq 'ClassA'}"> #{errorMessage1} </h:link> <h:link rendered="#{model['class'].simpleName eq 'ClassB'}"> #{errorMessage2} </h:link> Note the importance of

Why not use instanceof operator in OOP design?

白昼怎懂夜的黑 提交于 2019-12-17 06:13:44
问题 It has been repeatedly said that the instanceof operator should not be used except in the equals() method, otherwise it's a bad OOP design. Some wrote that this is a heavy operation, but it seems that, at least java, handles it pretty well (even more efficiently than Object.toString() comparison). Can someone please explain, or direct me to some article which explains why is it a bad design? Consider this: Class Man{ doThingsWithAnimals(List<Animal> animals){ for(Animal animal : animals){ if

Is instanceof considered bad practice? If so, under what circumstances is instanceof still preferable?

感情迁移 提交于 2019-12-17 03:35:02
问题 Over the years, I've tried to avoid instanceof whenever possible. Using polymorphism or the visitor pattern where applicable. I suppose it simply eases maintenance in some situations... Are there any other drawbacks that one should be aware of? I do however see it here and there in the Java libraries so I suppose it has its place? Under what circumstances is it preferable? Is it ever unavoidable? 回答1: I can imagine some cases, for example you have some objects of a library, which you can't

instanceof - incompatible conditional operand types

丶灬走出姿态 提交于 2019-12-17 01:01:33
问题 The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this doesn't: String s = new String(); System.out.println(s instanceof Cloneable); A compiler error is thrown. What is the problem? 回答1: A more blatant incarnation of your problem is the following: if ("foo" instanceof Number) // "Incompatible conditional operand types String and Number" This is specified in JLS 15.20.2 Type comparison operator instanceof: RelationalExpression:

js类型检测

此生再无相见时 提交于 2019-12-16 22:28:56
大约是三月初吧,在网上看到一道面试题,怎么判断一个变量类型是不是数组。然后从犀牛书以及查阅一些资料得到了答案。 这里分为四种情况分析: 通过constructor [].constructor === Array; //true 这种方法比较坑,不推荐,因为constructor是可以自己修改的。 通过instanceof [] instanceof Array; //true 犀牛书给出的解释是在页面中含多个窗体或者iframe,那么会产生很多执行环境,一个iframe下的数组不是另一个窗体下构造函数的实例。那么如果出现这种极端条件,instanceof也不推荐使用。 通过Array.isArray 在新版浏览器,IE9+都已经实现了原生方法 Array.isArray([1,2]); //true 通过toSting Object.prototype.toString.call([]) === '[object Array]' 在一些博客和犀牛书给出的都是这种方法。 因为之前对call语法,toString方法的疑惑,导致我只能死记硬背这句代码。最近在回顾继承有关知识,学到了call方法,这段代码意思是对[]调用Object对象原型下的toString方法,不同于数组原型下的toString的方法,之所以疑惑,因为数组下的toString是把一个数组转成字符串

What is the difference between instanceof and Class.isAssignableFrom(…)?

泄露秘密 提交于 2019-12-16 20:32:11
问题 Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give the same result? 回答1: When using instanceof , you need to know the class of B at compile time. When using isAssignableFrom() it can be dynamic and change during runtime. 回答2: instanceof can only be used with reference types, not primitive types.

Jetty - Container源码分析

这一生的挚爱 提交于 2019-12-16 17:51:09
1. 描述 Container提供管理bean的能力。 基于Jetty-9.4.8.v20171121。 1.1 API public interface Container { // 增加一个bean,如果bean是一个Container.Listener则隐含调用addEventListener(Container.Listener)方法 // Container.Listener只关心两个事件:(1)增加bean(2)删除bean public boolean addBean(Object o); // 返回该Container里面所有的bean public Collection<Object> getBeans(); // 返回指定类型(包括子类)的bean public <T> Collection<T> getBeans(Class<T> clazz); // 返回指定类型(包括子类)的第一个bean,如果不存在则返回null public <T> T getBean(Class<T> clazz); // 删除指定的bean,如果bean是一个Container.Listener,隐含调用removeEventListener(Container.Listener) public boolean removeBean(Object o); //

instanceof与typeof 运算符

ぃ、小莉子 提交于 2019-12-16 17:48:32
typeof 运算符 描述 返回一个用来表示表达式的数据类型的字符串。 语法 typeof [ ( ] expression [ ) ] ; expression 参数是需要查找类型信息的任意表达式 。 说明 typeof 运算符把类型信息当作字符串返回。 typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined." typeof 语法中的圆括号是可选项。 Js代码 alert( typeof (5)); //<SPAN>number</SPAN> alert( typeof ( true )); //<SPAN>boolean</SPAN> alert( typeof ( "abc" )); //<SPAN><SPAN>string</SPAN> /SPAN> alert(typeof (5)); //number alert(typeof (true)); //boolean alert(typeof ("abc")); //string --------------------------------------------------------------------------------------------------- instanceof 运算符 描述

1、JS的数据类型

耗尽温柔 提交于 2019-12-16 14:59:31
一、分类:JS有6种数据类型: 1.string 2.undefined 3.number 4.null 5.boolean 6.object(包含函数、数组、日期等) 二、隐式转换 1.+和- var x = "37" - 7 //30 运算 var y = "37" + 7 //377 字符串拼接 2.a == b "1.23" == 1.23; //会尝试将字符串转为数字比较 0 == false; //会将false转为数字0进行比较 null == undefined; new Object() == new Object(); [1,2] == [1,2]; object == number|string //尝试对象转换为基本类型,如: new String("hi")=="hi" //true 若用 a === b会先比较类型,类型不同,返回false 三、包装对象object 首先,有一个基本类型str和一个对象类型strObj,尝试进行赋值和获取 接下来,尝试查看str的长度,并给str加一个方法t,获取t 可以看到,长度可以获取,并且str.t = 10赋值成功,但是再获取str.t的时候却得到了undefined,无法获取。 ——这是因为,当把一个基本类型尝试转化为对象来时,浏览器会将其转换成一个相对应的 临时的 包装类型对象 ,当完成这个访问后

JS中的call、apply、bind方法详解

╄→гoц情女王★ 提交于 2019-12-16 10:37:24
文章目录 apply、call apply、call 区别 apply、call实例 数组之间追加 获取数组中的最大值和最小值 验证是否是数组(前提是toString()方法没有被重写过) 类(伪)数组使用数组方法 面试题 bind 绑定函数 部分函数(Partial Functions) 和setTimeout一起使用 绑定函数作为构造函数 捷径 实现 apply、call、bind比较 (^_^)打赏作者喝个咖啡(^_^) bind 是返回对应函数,便于稍后调用;apply 、call 则是立即调用 。 apply、call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向。 JavaScript 的一大特点是,函数存在「定义时上下文」和「运行时上下文」以及「上下文是可以改变的」这样的概念。 function Fruits() {} Fruits.prototype = { color: "red", say: function() { console.log("My color is " + this.color); } } var apple = new Fruits; apple.say(); //My color is red