Java:为什么我不能将其转换为对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:18:40  来源:igfitidea点击:

Java: Why can't I cast this to an Object?

javaobjectcasting

提问by jbu

This is bizarre... I thought every object in java had Object as an ancestor.

这很奇怪......我认为java中的每个对象都有Object作为祖先。

I have a ClassAthat extends my ClassBand implements Runnable.

我有一个ClassA扩展我的ClassB和实现的Runnable

After creating ClassAI cannot cast it to an Object.

创建后,ClassA我无法将其转换为Object.

Assume getClassAreturns a ClassAinstance.

假设getClassA返回一个ClassA实例。

I am doing

我在做

Object obj = getClassA();

I also tried

我也试过

Object obj = (Object) getClassA();

I get an incompatible types compile error: found Class, required Object.

我收到一个不兼容的类型编译错误:找到类,需要对象。

What's the deal with that? I thought all objects could be cast to Object.

那有什么关系?我认为所有对象都可以转换为 Object。

Edit: I assume it has something to do with the fact that ClassA implements Runnable, but I am not sure and need an explanation.

编辑:我认为这与 ClassA 实现 Runnable 的事实有关,但我不确定并且需要解释。

Edit2: Changing getClassA() to return an Object allows the program to compile.

Edit2: 改变 getClassA() 以返回一个 Object 允许程序编译。

Edit3: Importing the package that contained ClassB fixed the problem. Class B was defined in a different jar. ClassA was defined in another jar that referenced the jar containing ClassB.

Edit3:导入包含 ClassB 的包修复了问题。B 类是在不同的 jar 中定义的。ClassA 是在另一个引用包含 ClassB 的 jar 的 jar 中定义的。

回答by Pavel Minaev

Do you by chance have a class named Objectsomewhere in your code (or non-standard packages that it imports)?

您是否偶然Object在您的代码(或它导入的非标准包)中的某处命名了一个类?

回答by Arun P Johny

How you are compiling the java file. Can you give some more information about getClassA(). What is the return type of this method?

你是如何编译java文件的。你能提供更多关于 getClassA() 的信息吗?这个方法的返回类型是什么?

The type casting is unnecessary since all objects in java are of type Object.

类型转换是不必要的,因为 java 中的所有对象都是 Object 类型。

If you are using a IDE like eclipse you can put a break point on the line where you are assigning the Object obj = getClassA(); and inspect the value returned by getClassA().

如果您使用的是 Eclipse 之类的 IDE,您可以在分配 Object obj = getClassA(); 的行上放置一个断点;并检查 getClassA() 返回的值。

Else you can try to put a instanceof condition before assigning the value to obj.

否则,您可以尝试在将值分配给 obj 之前放置一个 instanceof 条件。

if(getClassA() instanceof Object){
    Object obj = getClassA();
}else{
    System.out.println("getClassA() is not retuning an object: "+ getClassA());
}

回答by Kevin Montrose

Class does descend from Object. There's something else going on here.

类确实从对象下降。这里还有其他事情发生。

If you're code really is:

如果你的代码真的是:

//...Code
Object obj = MyObject.getClassA();
//More Code...

class MyObject{
  static Class getClassA(){...}
}

It should work. Show us the code for an actual answer.

它应该工作。向我们展示实际答案的代码。

回答by MatrixFrog

I tried this in Eclipse and got an "unnecessary cast" warning. You can probably configure it so that is an error instead of a warning, so I would guess that's what you did.

我在 Eclipse 中尝试了这个并收到了“不必要的演员”警告。您可能可以将其配置为错误而不是警告,所以我猜这就是您所做的。