简体   繁体   English

返回类型与Object.getClass()不兼容

[英]Return Type incompatible with Object.getClass()

the code is generated upon importing a webservice file on eclipse. 该代码是在Eclipse上导入Web服务文件时生成的。 However, I saw this error upon compiling (return type incompatible with Object.getClass() ). 但是,我在编译时看到此错误(返回类型与Object.getClass()不兼容)。

Any ideas to fix this? 有任何解决办法吗?

public java.lang.String getClass(){
    return localClass;
}

Add On: 添加在:

   if (localClass != null){
                                            elementList.add(org.apache.axis2.databinding.utils.ConverterUtil.convertToString(localClass));
                                        } else {
                                           throw new org.apache.axis2.databinding.ADBException("Class cannot be null!!");
                                        }

I take it your web service is given as WSDL file. 我认为您的Web服务以WSDL文件形式给出。 From this WSDL file some Java files are automatically generated. 从此WSDL文件自动生成一些Java文件。 Apparently, your WSDL file contains a property named "class" and thus the corresponding generated Java class has a getter for this property called getClass() . 显然,您的WSDL文件包含一个名为“ class”的属性,因此,相应的生成的Java类对该属性具有名为getClass()的吸气剂。

However, getClass() is a method which is defined in the Object class and all Java classes inherit from Object . 但是, getClass()是在Object类中定义的方法,所有Java类都从Object继承。 Java thinks you are trying to override this method., which is not allowed, because 1) the method is final, and 2) the return type doesn't match. Java认为您正在尝试覆盖此方法。这是不允许的,因为1)该方法是最终方法,而2)返回类型不匹配。

You might want to look at the answer to this question , which mention how you can rename the property so that its getter doesn't conflict with standard Java methods. 您可能想看看这个问题的答案,其中提到了如何重命名该属性,以便其getter与标准Java方法不冲突。

getClass is a final method of Object which returns a Class . getClassObject的最终方法,该方法返回Class You are getting that error because the compiler thinks you are trying to override getClass while changing the return type to something that is not a Class . 之所以会出现此错误,是因为编译器认为您正在尝试重写getClass同时将返回类型更改为非Class If you want to return a String you will need to change the method name to something else or add parameters to the method so its signature does not match Object#getClass . 如果要返回String ,则需要将方法名称更改为其他名称或向方法添加参数,以使其签名与Object#getClass不匹配。 Note that you can't override it even if you want to return a Class because it is final. 请注意,即使您想返回Class ,也无法覆盖它,因为它是最终的。

 public  java.lang.String getClass(){
              return localClass;
   }

can be changed with 可以用

 public  Class<?> getClass(){
              return localClass;
 }

But as getClass is final method this cannot be overidden. 但是由于getClass是最终方法,因此不能忽略它。 If you want your own method then consider changing name. 如果您想要自己的方法,请考虑更改名称。

I would actually prefer: 我实际上更喜欢:

public final Class<? extends Object> getMyClass(){
    return localClass;
}

Which is generic. 这是通用的。 Since everything in Java inherits from Object, this matches everything loaded on the classpath. 由于Java中的所有内容都继承自Object,因此它与加载在类路径上的所有内容匹配。 And you probably have issues because you are trying to override the final method .getClass() (so the name here is getMyClass() instead. 您可能会遇到问题,因为您正尝试覆盖final方法.getClass() (因此,此处的名称为getMyClass()

From the javadoc of .getClass() you can see that: .getClass()的javadoc中,您可以看到:

The actual result type is Class<? extends |X|> 实际的结果类型是Class<? extends |X|> Class<? extends |X|> where |X| Class<? extends |X|>其中|X| is the erasure of the static type of the expression on which getClass is called. 是擦除在其上调用getClass的表达式的static类型。

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

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