为什么 parameter.getType().isInstance(HttpServletRequest.class) return 是假的,但是使用“==”是真的
why parameter.getType().isInstance(HttpServletRequest.class) return is false,but use "==" is true
Parameter[] ps = method.getParameters();
Map<String,Integer> map = new HashMap<String,Integer>();
for(int ij = 0;ij<ps.length;ij++){
Parameter p = ps[ij];
RequestParam rp = p.getAnnotation(RequestParam.class);
if(rp != null){
//do something
}else {
System.out.println(p.getType());
System.out.println(p.getType().isInstance(HttpServletRequest.class));
System.out.println(p.getType() == HttpServletRequest.class);
}
}
输出是:
interface javax.servlet.http.HttpServletRequest
false
true
为什么用"isInstance"是假,用“==”是真?
因为"instance of"无法判断implements关系?
该类型不是 HttpServletRequest
class 的实例,它是 java.lang.Class
的实例,其中包含有关 HttpServletRequest
class 的信息.
isInstance 等于 instanceOf
This method is the dynamic equivalent of the Java language instanceof
operator.
方法 return 错误,因为您正在将 class(return 由 p.getType() 编辑)与另一个 class HttpServletRequest.class 相反,此方法需要一个实例,例如:
Dog bobby = new BobbyDog(); // class BobbyDog extends Dog
System.out.println(Dog.class.isInstance(bobby)); // correct use (return true)
System.out.println(Dog.class.isInstance(BobbyDog.class)); // incorrect use (return false)
等于运算符return为真,因为两个class相等
p.getType() == HttpServletRequest.class // true
HttpServletRequest.class == HttpServletRequest.class // true
如果你想判断实现关系你必须使用方法
isAssignableFrom(Class<?> cls)
Determines if the class
or interface represented by this Class object is either the same as,
or is a superclass or superinterface of, the class or interface
represented by the specified Class parameter. It returns true if so;
otherwise it returns false. If this Class object represents a
primitive type, this method returns true if the specified Class
parameter is exactly this Class object; otherwise it returns false.
Parameter[] ps = method.getParameters();
Map<String,Integer> map = new HashMap<String,Integer>();
for(int ij = 0;ij<ps.length;ij++){
Parameter p = ps[ij];
RequestParam rp = p.getAnnotation(RequestParam.class);
if(rp != null){
//do something
}else {
System.out.println(p.getType());
System.out.println(p.getType().isInstance(HttpServletRequest.class));
System.out.println(p.getType() == HttpServletRequest.class);
}
}
输出是:
interface javax.servlet.http.HttpServletRequest
false
true
为什么用"isInstance"是假,用“==”是真? 因为"instance of"无法判断implements关系?
该类型不是 HttpServletRequest
class 的实例,它是 java.lang.Class
的实例,其中包含有关 HttpServletRequest
class 的信息.
isInstance 等于 instanceOf
This method is the dynamic equivalent of the Java language instanceof operator.
方法 return 错误,因为您正在将 class(return 由 p.getType() 编辑)与另一个 class HttpServletRequest.class 相反,此方法需要一个实例,例如:
Dog bobby = new BobbyDog(); // class BobbyDog extends Dog
System.out.println(Dog.class.isInstance(bobby)); // correct use (return true)
System.out.println(Dog.class.isInstance(BobbyDog.class)); // incorrect use (return false)
等于运算符return为真,因为两个class相等
p.getType() == HttpServletRequest.class // true
HttpServletRequest.class == HttpServletRequest.class // true
如果你想判断实现关系你必须使用方法
isAssignableFrom(Class<?> cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.