简体   繁体   English

Java中的类等效

[英]Class equivalence in java

A small hiccup led me to a basic question. 小小的打ic使我想到了一个基本问题。 I intend to check the class equivalence in the following case (academic curiosity - Please do not direct me to instanceof or isinstance ): 我打算在以下情况下检查班级对等情况(学术上的好奇心-请不要引导我前往instanceofisinstance ):

public class staticChild extends staticParent{
    ....
        public static void main(String[] args){
        staticChild thisobj=new staticChild();
        System.out.println(thisobj.getClass()==staticParent.class);
    }

}

This gives compilation error: 这给出了编译错误:

error: incomparable types: Class<CAP#1> and Class<staticParent>
                System.out.println(thisobj.getClass()==staticParent.class);
 where CAP#1 is a fresh type-variable: CAP#1 extends staticChild from capture of ? extends staticChild
  1. What does this mean? 这是什么意思?
  2. What is the difference between type and class(specifically to this context - what is the type and class of staticchild)? type和class之间有什么区别(特定于此上下文-staticchild的类型和类是什么)?
  3. How do I get rid of the compilation error? 我如何摆脱编译错误?

Since thisobj is declared to be of type staticChild , the expression: 由于thisobj被声明为staticChild类型,因此表达式:

thisobj.getClass()

returns an object of type Class<? extends staticChild> 返回Class<? extends staticChild>类型的对象 Class<? extends staticChild> . Class<? extends staticChild> You are trying to compare that to an object of type Class<staticParent> but the compiler cannot match the generic type parameters. 您试图将其与Class<staticParent>类型的对象进行比较,但编译器无法匹配泛型类型参数。 It knows that staticParent cannot possibly extend staticChild so there's nothing it can assign to ? 它知道staticParent不可能延长staticChild所以没有什么可分配给? in Class<? extends staticChild> Class<? extends staticChild> Class<? extends staticChild> . Class<? extends staticChild>

There are several ways to get rid of the error. 有几种方法可以消除该错误。 For instance, you could declare thisobj to be of type Object (or even of type staticParent ). 举例来说,你可以声明thisobj为类型的Object (甚至是类型staticParent )。

Note, however, that the test will always fail. 但是请注意,测试将始终失败。

  1. It means that the two objects you are trying to compare can never be equal because their types are not convertible to each other. 这意味着您要比较的两个对象永远不会相等,因为它们的类型不能相互转换。 According to JLS 15.21.3 , 根据JLS 15.21.3

It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). 如果无法通过强制转换将任何一个操作数的类型转换为另一个操作数的类型,则将发生编译时错误。 The run-time values of the two operands would necessarily be unequal. 两个操作数的运行时值必然不相等。

  1. There is no difference between type and class in this context. 在这种情况下,类型和类之间没有区别。

  2. Do not try to compare two objects that are not convertible to one another. 不要尝试比较两个不能相互转换的对象。 In this case you are comparing Class<CAP#1> with Class<staticParent> . 在这种情况下,您正在将Class<CAP#1>Class<staticParent> However, if you wanted to "fix" the error, you could use .equals instead, with the understanding that it would always return false . 但是,如果您想“修复”错误,可以使用.equals代替,但.equals是它将始终返回false

     System.out.println(thisobj.getClass().equals(staticParent.class)); 

The compiler is already smart enough to say classes are not equivalent. 编译器已经足够聪明,可以说类并不等效。 It assumes you are writing a usual program, not solving an academic task, and tries to assist at its best. 它假定您正在编写常规程序,而不是解决学术任务,并尝试提供最佳帮助。

If you still need to do this manually "because of Academic curiosities", assign them to the general variable of type Class first: 如果仍然由于“学术好奇心”而需要手动执行此操作,请将它们首先分配给Class类型的常规变量:

Class a = staticChild.class;
Class b = staticParent.class;

if (a == b) // false

(will show "raw type" warning but should compile). (将显示“原始类型”警告,但应编译)。

Object x = a.newInstance();
Object y = b.newInstance();

Rest is up to you. 休息由您决定。

It means you have an error in your code. 这意味着您的代码中有错误。

The error indicates that you have incompatible types. 该错误表明您具有不兼容的类型。 Yes, you can see it from the error message, but what incompatible types. 是的,您可以从错误消息中看到它,但是类型不兼容。 You are comparing two classes via "==". 您正在通过“ ==”比较两个类。

If you are trying to check if thisobj is an instance of staticParent then you should use: 如果尝试检查thisobj是否为staticParent的实例,则应使用:

System.out.println(thisobj instanceof staticParent);

But specifically the error is because you are running a comparison for true/false based on two classes that can never be equal. 但具体来说,该错误是因为您基于两个永远不会相等的类对真/假进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM