int.class.isInstance(Object)是矛盾的吗? [英] Is int.class.isInstance(Object) a contradiction?

查看:82
本文介绍了int.class.isInstance(Object)是矛盾的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子:

public boolean check(Class<?> clazz, Object o)
{
    return clazz.isInstance(o);
}

check(int.class, 7); // returns false

由于isInstance接受Object,因此它不适用于int,因为int是原始类型,并且自动装箱到Integer. 那么,有没有可能编写一个通用的检查方法?还是我应该确保 clazz是Class<? extends Object>类型?

Since isInstance accepts an Object, it won't work with int, because int is a primitive type and gets autoboxed to Integer. So is it at all possible to write a generic check method? Or should I make sure that clazz is of type Class<? extends Object>?

推荐答案

并非所有Class对象都代表类/引用类型;还有Class对象代表原始类型.这很有用,因为在将反射与字段和方法一起使用时,您通常需要指定其类型,并且它可以是原始类型.因此Class用于表示所有此类泛型类型.

Not all Class objects represent classes / reference types; there are also Class objects that represent primitive types. This is useful because in using reflection with fields and methods, you often need to specify their type, and it can be a primitive type. So Class is used to represent all such pre-generics types.

但是,Class类的许多方法对于基本类型都没有意义.例如,对象不可能是instanceof int.因此,类似的.isInstance()方法将始终返回false.由于该方法的参数类型为Object,因此从语言的角度来看,传递给您的原始类型根本就是不可能的.

However, many of the methods of the Class class do not make sense for primitive types. For example, it is impossible for an object to be instanceof int. Therefore, the analogous .isInstance() method will always return false. Since the parameter of that method is type Object, it is simply impossible from a language point of view for what you pass in there to be of a primitive type.

当然,在Java 5+中,当您将原语传递给类型为Object的参数时,它会进行自动装箱,但是对它进行自动装箱的事实意味着,所传递的实际上是一个引用.到一个对象.引用类型和原始类型是不同的.参数是引用类型或原始类型.因此,您无法编写可以采用引用或原始"的方法.

Sure, in Java 5+ when you pass a primitive to a parameter of type Object, it undergoes autoboxing, but the fact that it underwent autoboxing means that what was passed is actually a reference to an object. Reference types and primitive types are distinct. A parameter is either a reference type or primitive type. Thus you cannot write a method that can take a "reference or primitive".

在您的示例中,您可能要问的是检测对象是否已从基元自动装箱,并将其与基元类型进行比较.但是,无法检测到呼叫者是否对其进行了自动装箱,因为自动装箱是呼叫之前完全在呼叫方进行的操作.

What you may be asking, in your example, is to detect that the object was autoboxed from a primitive, and compare it to a primitive type. However, it is impossible to detect whether the caller autoboxed it, since autoboxing is a completely caller-side operation that happens before the call.

但是,假设它是自动装箱的,您知道它应该输入哪种类型.如果您期望int,并且它已自动装箱并传递给您的方法,则它应该是Integer的实例.因此,您可以做的是,当clazz表示原始类型时,改为对其包装类进行检查.因此,当看到clazzint.class时,用Integer.class代替它,然后执行检查.请注意,这种方式仍然无法确定是否自动装箱了o参数时传递的内容.

However, assuming it was autoboxed, you know what type it should have gone to. If you are expecting an int, and it is autoboxed and passed to your method, it should be an instance of Integer. Thus, what you could do is, when clazz represents a primitive type, instead perform the check on its wrapper class. Thus, when it sees that clazz is int.class, substitute it with Integer.class, and then perform the check. Note that this way still doesn't tell whether what was passed as the o parameter was autoboxed.

这篇关于int.class.isInstance(Object)是矛盾的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆