instanceof

instanceof keyword usage

≯℡__Kan透↙ 提交于 2019-12-04 18:39:30
问题 Is using the instanceof keyword against the essence of object oriented programming ? I mean is it a bad programming practice? I read somewhere that using instanceof keyword means that the design may not be that good. Any better workaround? 回答1: Generally speaking yes. It's best to keep all code that depends on being a specific class within that class, and using instanceof generally means that you've put some code outside that class. Look at this very simple example: public class Animal { }

instanceof

ぃ、小莉子 提交于 2019-12-04 13:31:15
如何才能知道一个父类引用的对象,本来是什么子类? 格式: 对象 instanceof 类名称 这将对得到一个boolean值结果,也就是判断前面的对象不能当作后面类型的实例 向下转型需要使用instanceof 否则可能出现类转换异常 来源: https://www.cnblogs.com/Damocless/p/11867789.html

Reflection: cast an object to subclass without use instanceof

落花浮王杯 提交于 2019-12-04 11:23:18
I have this simple interface/class: public abstract class Message {} public class Message1 extends Message {} public class Message2 extends Message {} And an utility class: public class Utility { public void handler(Message m) { System.out.println("Interface: Message"); } public void handler(Message1 m) { System.out.println("Class: Message1"); } public void handler(Message2 m) { System.out.println("Class: Message2"); } } Now, the main class: public static void main(String[] args) { Utility p = new Utility(); Message1 m1 = new Message1(); p.handler(m1); Message m = (Message) m1; p.handler(m); }

javascript中判断数据类型

最后都变了- 提交于 2019-12-04 10:32:41
编写javascript代码的时候常常要判断变量,字面量的类型,可以用typeof,instanceof,Array.isArray(),等方法,究竟哪一种最方便,最实用,最省心呢?本问探讨这个问题。 1. typeof 1.1 语法 typeof返回一个字符串,表示未经计算的操作数的类型。 语法:typeof(operand) | typeof operand 参数:一个表示对象或原始值的表达式,其类型将被返回 描述:typeof可能返回的值如下: 类型 结果 Undefined “undefined” Null “object” Boolean “boolean” Number “number” Bigint “bigint” String “string” Symbol “symbol” 宿主对象(由JS环境提供) 取决于具体实现 Function对象 “function” 其他任何对象 “object” 从定义和描述上来看,这个语法可以判断出很多的数据类型,但是仔细观察,typeof null居然返回的是“object”,让人摸不着头脑,下面会具体介绍,先看看这个效果: // 数值 console.log(typeof 37) // number console.log(typeof 3.14) // number console.log(typeof(42)) //

How do I make a class, which I can't change, implement an interface?

吃可爱长大的小学妹 提交于 2019-12-04 09:44:41
问题 I have a class from another library that is closed-source, but I want to be able to use an interface for it. The reason being that I don't want to do instanceof checks or null -checks everywhere, but I also don't want to extend the existing class. For example, let's say I have this code: public class Example { // QuietFoo is from another library that I can't change private static QuietFoo quietFoo; // LoudFoo is my own code and is meant to replace QuietFoo private static LoudFoo loudFoo;

GoJS组织结构图

我的未来我决定 提交于 2019-12-04 09:02:22
<!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"> function init() { if (window.goSamples) goSamples(); // 这些样本的初始化-您无需调用 var $ = go.GraphObject.make; // 为了简洁定义模板 myDiagram = $(go.Diagram, "myDiagramDiv", // 必须是div的ID或引用 { maxSelectionCount: 1, // 用户一次只能选择一个零件 validCycle: go.Diagram

See if two object have the same type

巧了我就是萌 提交于 2019-12-04 08:19:20
问题 Let's say that I have a class A, and that B,C,D are derived from A. If I want to know what's the type of an object referenced, I can declare: // pseudo-code if(obj instanceof B) < is B> else if(obj instanceof C) < is C> else <is D> This because I am sure that the classes derived from A are only B,C and D. But what if I want just to check that two references point to the same kind of object? So something like: if(obj1 instanceof obj2) <do something> But of course the syntax is wrong.How to

1.js的继承的实现方法

大憨熊 提交于 2019-12-04 08:12:31
原文摘自: https://www.cnblogs.com/gwf93/p/10384352.html 先写一个父类Car function Car(name){ this.name = name; this.driver = function (){ console.log('日行千里') } } Car.prototype.addOil = function(param){ this.oil = param; console.log(this.name + '每天加油' + param + 'L') } (一)原型链继承:    核心: 将父类的实例作为子类的原型 instanceOf:测试一个对象是否为一个类的实例 // 原型继承 function BaoMa() { } BaoMa.prototype = new Car() //父类的实例作为子类的原型,继承的关键 BaoMa.prototype.name = "宝马" var baoMa1 = new BaoMa(); // 子类的实例化 console.log(baoMa1.name) console.log(baoMa1.addOil(20)) console.log(baoMa1.driver()) console.log(baoMa1 instanceof Car) // true console.log

js 判断变量的数据类型

烂漫一生 提交于 2019-12-04 06:25:36
js 里判断变量类型大概分为三种方法 1. typeof 比较常用的 先排除几个特殊情况,之后的逻辑可能就能清晰点如下 1 // 特殊的基本数据类型判断 2 typeof null // 'object' 3 // 特殊的引入数据类型判断 4 typeof function () {} // 'function' 5 typeof new Function() // 'function' 剩下的就是基本数据类型会返回其数据类型,其他引用数据类型包括 new 关键字定义的变量,如下 1 // 基本数据类型判断 2 typeof 1 // 'number' 3 typeof 'a' // 'string' 4 typeof false // 'boolean' 5 typeof undefined // 'undefined' 6 typeof Symbol() // 'symbol' 7 8 // 引用数据类型判断和new关键字创建的变量判断类型 9 typeof {} // 'object' 10 typeof [] // 'object' 11 typeof new Number(1) // 'object' 12 typeof new String('a') // 'object' 13 typeof new Boolean(true) // 'object' 14

Is this an acceptable use of instanceof?

戏子无情 提交于 2019-12-04 05:30:34
问题 I am working on an implementation of the card game UNO (mostly to run simulations to test some house rules, but that's a different story). For those who've never played it, it's similar to Crazy Eights. Players take turns playing a card on the discard pile. The card has to match in number or color. There are also draw cards, which force the next player to draw either two or four cards. It's also quite friendly to house rules, which makes it pretty interesting. I have a Card class with the