instanceof

How to test if one java class extends another at runtime?

↘锁芯ラ 提交于 2019-11-27 00:00:55
How to I test if a is a subclass of b ? Class<?> a = A.class; Class<?> b = B.class; Are you looking for: Super.class.isAssignableFrom(Sub.class) Rob Hruska If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class) . For your example, it would be: if(B.class.isAssignableFrom(A.class)) { ... } If you're interested in whether or not an instance is of a particular type, use instanceof : A obj = new A(); if(obj instanceof B) { ... } Note that these will return true if the class/instance is a member of the type hierarchy and are not restrictive to direct

Calling method that exists in child classes but not in parent class

爷,独闯天下 提交于 2019-11-26 23:35:58
问题 public class Parent { .... } public class Child1 extends Parent { .... public void foo() { .... } } public class Child2 extends Parent { .... public void foo() { .... } } Here method foo() only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo() method on obj which is Parent class's reference then I need to use intanceof with multiple if..else which I want to avoid. Parent obj = ...// Object of one of

Why not use instanceof operator in OOP design?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:36:16
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(animal instanceOf Fish){ eatIt(animal); } else if(animal instanceof Dog){ playWithIt(animal); } } } ...

Why can a constructor only return an object?

空扰寡人 提交于 2019-11-26 22:04:10
问题 If there is a constructor like function a() {} then (new a) instanceof a === true But on the other hand, function a() { return {} } results in (new a) instanceof a === false So what I was thinking is that function a() { return 123 } would result in the same thing. However, when returning a Number, (new a) instanceof a === true How is this possible? Why can't I make a constructor return something else than an Object? (I do know making a constructor returning a Number is rather useless but I

hibernate 反射框架(自用)

痞子三分冷 提交于 2019-11-26 21:49:21
hibernate 只需要操作对象就可以对数据库的数据进行“增删改查”。用了短时间后,感觉依旧存在很大的冗余。正因为这个,我的反射框架就出现了。因为自用,下面只贴出代码,不做解释。有兴趣的可以来看看一起研究一下,如有问题可私聊探讨。 反射基类 SQLSuper /** * 给对象做反射并且定于返回HQL语句方法的抽象类 * @author vincent Mao * */ public abstract class SQLSuper { /** * */ protected StringBuffer SQL; public StringBuffer getSQL() { return SQL; } public void setSQL(StringBuffer sQL) { SQL = sQL; } /** * 根据传入的实体对象and条件集合and排序对象返回HQL语句 * @param obj 经过重新封装的实体类 * @param condition 条件集合 * @param orderBy 排序对象 * @return HQL */ public abstract String getSQL(Object obj , List condition,OrderBy orderBy); /** * 返回所有类的名字 * @param tables * @return List

protobuf使用

假如想象 提交于 2019-11-26 20:27:52
一、protobuf环境搭建 Github 地址: https://github.com/protocolbuffers/protobuf 然后进入下载页 https://github.com/protocolbuffers/protobuf/releases 下载系统对应版本编译器 我这里使用的是window 64位 下好之后解压,然后把bin里面的protoc.exe加入到环境变量, 并且把protoc.exe拷贝到C:\Windows\System32 二、创建Student.proto 文件 syntax = "proto2"; package com.example.protobuf; option optimize_for = SPEED; option java_package = "com.example.protobuf"; option java_outer_classname = "DataInfo"; message Student{ required string name = 1; optional int32 age = 2; optional string address = 3; }    在工程中增加依赖 <dependency> <groupId>com.google.protobuf</groupId> <artifactId

Why does 'instanceof' in TypeScript give me the error “'Foo' only refers to a type, but is being used as a value here.”?

末鹿安然 提交于 2019-11-26 19:47:40
问题 I wrote this code interface Foo { abcdef: number; } let x: Foo | string; if (x instanceof Foo) { // ... } But TypeScript gave me this error: 'Foo' only refers to a type, but is being used as a value here. Why is this happening? I thought that instanceof could check whether my value has a given type, but TypeScript seems not to like this. 回答1: What's going on The issue is that instanceof is a construct from JavaScript, and in JavaScript, instanceof expects a value for the right-side operand.

The 'instanceof' operator behaves differently for interfaces and classes

匆匆过客 提交于 2019-11-26 19:47:22
问题 I would like to know regarding following behavior of instanceof operator in Java. interface C {} class B {} public class A { public static void main(String args[]) { B obj = new B(); System.out.println(obj instanceof A); //Gives compiler error System.out.println(obj instanceof C); //Gives false as output } } Why is it so? There is no relation between interface C and class B , but it gives false whereas in case of obj instanceof A it gives compiler error? 回答1: Because Java has no multiple

Test if object is instanceof a parameter type

风格不统一 提交于 2019-11-26 19:26:52
问题 Is there a way to determine if an object is an instance of a generic type? public <T> test(Object obj) { if (obj instanceof T) { ... } } That clearly doesn't work. Is there an alternative? Like I want to use Java reflection to instantiate a class and then check to make sure it is of type generic T . 回答1: The only way you can do this check is if you have the Class object representing the type: Class<T> type; //maybe passed into the method if ( type.isInstance(obj) ) { //... } 回答2: To extend