instanceof

Test if object is instanceof a parameter type

谁说胖子不能爱 提交于 2019-11-27 18:39:26
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 . 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) ) { //... } To extend the sample of Mark Peters, often you want to do something like: Class<T> type; //maybe passed to the method if (

Elasticsearch Date类型,时间存储相关说明

寵の児 提交于 2019-11-27 13:28:46
从昨晚开始,到今天中午之前,一直在纠结时间存储问题,昨晚是纠结时间取出来的问题。 其实我的想法很简单,我就想 java.util.Date 存储到 Elasticsearch ,然后从 Elasticsearch 中再取出来的时候,它是个 Date ,不需要我任何转换。 但是发现好像不行。 我开始在创建 Mapping 的时候,就是为: //...省略部分代码 .startObject("create_date").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").endObject() //...省略部分代码 指定了 Type 为 Date ,并且 format 为 yyyy-MM-dd HH:mm:ss ,然后 new Date(); 插入后报错: message [MapperParsingException[failed to parse [create_date]]; nested: IllegalArgumentException[Invalid format: "2016-07-04T03:03:12.616Z" is malformed at "T03:03:12.616Z"];] 根据错误提示,我先把时间格式化,然后插入: result.put("create_date", new

Check if a object is a instance of a class (but not a instance of its subclass)

独自空忆成欢 提交于 2019-11-27 13:26:35
For this example: public class Foo{} public class Bar extends Foo{} .... void myMethod(Foo qux){ if (checkInstance(qux,Foo.class)){ .... } } How can I check if qux is a instance of Foo (but not a instance of its subclass of foo)? That is: checkInstance(qux,Foo.class)=true checkInstance(qux,Bar.class)=false Is there some kind of statement like instanceof for this check? or I should use qux.getClass().equals(Foo.class) If you have to do this, the only way would be the getClass().equals(Foo.class) option you've suggested. However, the goal of OO design is to allow you to treat any Foo in the same

Is it good practice to often use instanceof?

北城以北 提交于 2019-11-27 13:15:41
问题 The scenario. I'm writting game-related code. In that game a Player (its also a class) has a list of Item . There are other types of items that inherit from Item , for example ContainerItem , DurableItem or WeaponItem . Obviously it is very conveniant for me to just have List<Item> . But when I get the players items, the only way for me to distinguish between what type of item is by using the instanceof keyword. I'm sure I've read that reliaing on it is bad practice. Is it ok to use it in

instanceof check in EL expression language

五迷三道 提交于 2019-11-27 13:05:01
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> 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 specifying Object#getClass() with brace notation ['class'] because class is a reserved Java literal which

android分享弹框、时间格式、shared工具类

元气小坏坏 提交于 2019-11-27 13:02:07
1、分享工具类: public class ShareUtils { public static void shareText(Context context, String shareText) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); intent.putExtra(Intent.EXTRA_TEXT, shareText); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(Intent.createChooser(intent, "分享")); } public static void shareImage(Context context, Uri uri) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.setType("image/jpeg");

What is the C# equivalent to Java's isInstance()?

心已入冬 提交于 2019-11-27 11:24:27
问题 I know of is and as for instanceof , but what about the reflective isInstance() method? 回答1: The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows: bool result = obj.GetType().IsAssignableFrom(otherObj.GetType()); Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type ) of an obj (via .getClass() vs .getType() ), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System

Java: instanceof Generic

懵懂的女人 提交于 2019-11-27 10:48:03
问题 Isn't there any way to find the class-type of a generic? if (T instanceof String) { // do something... } The above definitely does not compile. 回答1: Generics are a compile time feature. Generics add checks at compile time which may not have any meaning at runtime. This is one example. You can only check the type of the object referenced which could be a super type in code. If you want to pass the type T you have do this explicitly. void someMethod(Class<T> tClass) { if(String.class

JavaScript 的数据类型

坚强是说给别人听的谎言 提交于 2019-11-27 08:03:05
一、分类   根据 JavaScript 中的变量类型传递方式,分为基本数据类型和引用数据类型。其中基本数据类型包括Undefined、Null、Boolean、Number、String、Symbol (ES6新增,表示独一无二的值),而引用数据类型统称为Object对象,主要包括对象、数组和函数。 二、区别   在参数传递方式上,有所不同: 函数的参数如果是简单类型,会将一个值类型的数值副本传到函数内部,函数内部不影响函数外部传递的参数变量 如果是一个参数是引用类型,会将引用类型的地址值复制给传入函数的参数,函数内部修改会影响传递参数的引用对象。    基本数据类型和引用数据类型的区别: 基本类型和引用类型存储于内存的位置不同,基本类型直接存储在栈中。 引用类型的对象存储在堆中,与此同时,在栈中存储了指针,而这个指针指向正是堆中实体的起始位置。 三、举例   下面通过一个小题目,来看下两者的主要区别: // 基本类型 var a = 10 var b = a b = 20 console.log(a) // 10 console.log(b) // 20   上述代码中,a b都是值类型,两者分别修改赋值,相互之间没有任何影响。再看引用类型的例子: // 引用类型 var a = {x: 10, y: 20} var b = a b.x = 100 b.y = 200

What's the point of new String(“x”) in JavaScript?

会有一股神秘感。 提交于 2019-11-27 07:58:08
What are the use cases for doing new String("already a string") ? What's the whole point of it? There's very little practical use for String objects as created by new String("foo") . The only advantage a String object has over a primitive string value is that as an object it can store properties: var str = "foo"; str.prop = "bar"; alert(str.prop); // undefined var str = new String("foo"); str.prop = "bar"; alert(str.prop); // "bar" If you're unsure of what values can be passed to your code then I would suggest you have larger problems in your project. No native JavaScript object, major library