instanceof

When “if else”/“instance of” are inevitable, how do we improve the design apart from using visitor pattern?

有些话、适合烂在心里 提交于 2019-12-05 08:27:14
When we have an object hierarchy that is purely a inheritance of semantic and not of behaviors,then inevitably we need to write "instanceof" or "if/else" everywhere to do run time type checking. E.g. If I have a object hierarchy which has Class Function Class Average extends Function Class Sum extends Function Class Max extends Function If there is a method called calculate() in these classes, then we do not have problem, we can just take the advantage of polymorphism and this design satisfies the LSP. However what if we do not want to add this calculate() method to this hierarchy for some

PHP check for instance of DateTime?

扶醉桌前 提交于 2019-12-05 08:21:12
问题 Is this the only way to check if an object is an instance of a class, in my case of the DateTime class? $cls = ReflectionClass("DateTime"); if (! $cls->isInstance( (object) $var ) ) { // is not an instance } It seems a bit heavy to me. 回答1: You could try instanceof­Docs... if ($var instanceof DateTime) { // true } See also is_a­Docs: if (is_a($var, 'DateTime')) { // true } 回答2: if ($var instanceof DateTime) 回答3: You can use get_class function like this: <?php $a = new DateTime(); if (get

Java: Unchecked cast from X to Y / how to implement castOrNull

强颜欢笑 提交于 2019-12-05 07:06:49
I have implemented this function: static <X,Y> Y castOrNull(X obj) { try { return (Y)obj; } catch(ClassCastException e) { return null; } } This gives me the compiler warning: Type safety: Unchecked cast from X to Y Which I don't exactly understand. Isn't the try/catch which I am doing here a check for it? Can I ignore the warning? Will my function work as expected or not? How would I implement it correctly? I also tried with a obj instanceof Y check but that doesn't work because of the way Java handle generics. Btw., this function seems quite useful to me (to make some other code more clean).

GoJS组织结构图2

↘锁芯ラ 提交于 2019-12-05 02:34:45
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Org Chart Editor</title> <meta name="description" content="组织结构图编辑器-编辑详细信息并更改关系。" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://unpkg.com/gojs/release/go-debug.js"></script> <link rel="stylesheet" href="../extensions/dataInspector.css" /> <script id="code"> // 初始化JSON格式的数据 var modelObj = { "class": "go.TreeModel", "nodeDataArray": [{ "key": 1, "name": "Stella Payne Diaz" }, { "key": 2, "name": "Luke Warm", "parent": 1 }, { "key": 3, "name": "Meg Meehan Hoffa", "parent": 2 }, { "key": 4,

Halo(三)

耗尽温柔 提交于 2019-12-05 01:51:25
接口中可以定义方法 1. 定义静态方法(直接调用) public interface Test { public static void method() { /** * 1、定义一个静态的带有方法体的方法 * 2、接口不能创建对象,调用静态方法不需要对象 * 3、接口名调用 */ System.out.println("接口中静态方法"); } } 2. 定义普通方法(需要子类实现接口) public default void methodDefault(){ System.out.println(); } isAssignableFrom() 方法与 instanceof 判断继承关系 isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角度去判断。 isAssignableFrom()方法是判断是否为某个类的父类,instanceof关键字是判断是否某个类的子类。 使用方法: 父类.class.isAssignableFrom(子类.class) 子类实例 instanceof 父类类型 Enum 枚举类 Enum.valueOf(enumType, name.toUpperCase()):将枚举常量名称转换成枚举常量 参数: enumType -- 这是枚举类型,一个常量的类的对象。 name -- 这是枚举常量名称。

javascript typeof instanceof

倖福魔咒の 提交于 2019-12-05 00:19:24
typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),undefined。 instanceof用于判断一个变量是否某个对象的实例,主要是沿着 prototype这个对象查找,如果在这条链上能查找到就返回true,否则返回false. function proto() { this.getYear = function () { return 123; } } function obj() { this.name = "test!"; }; obj.prototype = new proto(); var ob = new obj(); console.log(ob instanceof Object); //true console.log(ob instanceof Date); //false console.log(ob instanceof proto); //true 来源: https://www.cnblogs.com/huaan011/p/11890904.html

Difference between RTTI and reflection in Java

做~自己de王妃 提交于 2019-12-04 23:00:19
问题 My question is when how does the class info gets loaded during runtime? When someone calls instanceof is that considered RTTI or reflection? Or it depends on the actual situation? 回答1: The term "RTTI" is a C++-specific term referring to the functionality of the core language that allows the program to determine the dynamic types of various objects at runtime. It usually refers to the dynamic_cast or typeid operators, along with the associated std::type_info object produced by typeid . The

学习笔记|Symbol

懵懂的女人 提交于 2019-12-04 20:41:23
1.概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突。比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突。如果有一种机制,保证每个属性的名字都是独一无二的就好了,这样就从根本上防止属性名的冲突。这就是 ES6 引入Symbol的原因。 ES6 引入了一种新的原始数据类型Symbol,表示独一无二的值。它是 JavaScript 语言的第七种数据类型,前六种是:undefined、null、布尔值(Boolean)、字符串(String)、数值(Number)、对象(Object)。 Symbol 值通过Symbol函数生成。这就是说,对象的属性名现在可以有两种类型,一种是原来就有的字符串,另一种就是新增的 Symbol 类型。凡是属性名属于 Symbol 类型,就都是独一无二的,可以保证不会与其他属性名产生冲突。 let s = Symbol(); typeof s // "symbol" 上面代码中,变量s就是一个独一无二的值。typeof运算符的结果,表明变量s是 Symbol 数据类型,而不是字符串之类的其他类型。 注意,Symbol函数前不能使用new命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象。也就是说,由于 Symbol 值不是对象,所以不能添加属性

Avoiding instanceof when checking a message type

删除回忆录丶 提交于 2019-12-04 20:17:15
问题 I have the following situation where a client class executes different behavior based on the type of message it receives. I'm wondering if there is a better way of doing this since I don't like the instanceof and the if statements. One thing I thought of doing was pulling the methods out of the client class and putting them into the messages. I would put a method like process() in the IMessage interface and then put the message specific behavior in each of the concrete message types. This

Check for an instance of ArrayBufferView?

半世苍凉 提交于 2019-12-04 19:31:23
问题 Background With a bit of research I've found that, although ArrayBufferView wasn't initially exposed (through [NoInterfaceObject]) there appeared to be broad agreement that it should be, due to my described use case. Firefox Chrome Safari The initial agreement was to expose the ArrayBufferView constructor on the DOMWindow namespace, which was implemented in Safari (and still works in 6.1.1) and Chrome, but was then pulled from Chrome in favour of a static method ArrayBuffer.isView() .