java.lang.ClassException:A不能转换为B [英] java.lang.ClassException: A cannot be cast into B

查看:320
本文介绍了java.lang.ClassException:A不能转换为B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了这段代码:

  A类{
//一些代码
}
class B扩展A {
//一些代码
}

class C {
public static void main(String [] args)
{
B b1 =(B)新A();
A a1 =(B)新A();
}
}

这两行在单独编译时编译正常,但是使用 java.lang.ClassException给出运行时错误
:A不能转换为B



为什么编译得好,但是给出了运行时错误?

解决方案

你需要明白类型 A 的引用可以存储类 A 的任何对象或扩展 A <的类/ code>,就像你的情况一样 B



所以有可能

  A a = new B(); 

由于 a 引用下的对象是实例class B 应该可以以某种方式将它存储在更准确的引用中,例如 B b 。所以让我们这样试试:

  B b = a; //类型不匹配错误

此类代码为我们编译时间类型不匹配错误,以防止出现这种情况:< br>
假设我们已经




  • 类B1扩展A 并且

  • B2类扩展A



我们创建了 A a =新B1()。现在如果编译器不会抛出类型不匹配错误 B1 b = a 它也应该允许 B2 b = a 但在 a 下有 B1 的实例,其中没有任何内容与 B2 相关。



告诉编译器我们知道潜在的类型不匹配,我们需要明确使用铸造到我们想要的班级

  B b =(B)a; 

这就是投放参考 A 可以在编译时加上扩展A






在你的代码中你有

  B b1 =(B)new A(); 
A a1 =(B)新A();

您需要知道 new 运算符是返回与创建对象相同类型的引用,因此 new A()将返回 A 类型的引用所以

  B b1 =(B)new A(); 

实际上与



$ b <$ p相同$ p> A tmp = new A();
B b1 =(B)tmp;

这里的问题是你不能存储某个类的对象来引用它的派生类。为什么?如果派生类具有该对象类没有的新方法,那该怎么办?

  A类{
//一些代码
}

B类扩展A {
private int i;
public void setI(int i){
this.i = i;
}
}

以及


$之后b $ b

  B b =(B)new A(); 

您将尝试调用 b.setI(42); ?会有可能吗?不,因为A类的实例没有方法 setI ,即使id有,它也没有字段 int i 用于此方法。



这就是为什么(B)新的A(); 抛出 java .lang.ClassCastException


I implemented this code:

class A {
    //some code
}
class B extends A {
    // some code
}

class C {
    public static void main(String []args)
    {
        B b1 = (B) new A();
        A a1 = (B) new A();
    }
}

Both of these lines, when compiled separately, compile fine,but give runtime error with java.lang.ClassException: A cannot be cast into B.

Why they compile well, but give a runtime error?

解决方案

You need to understand that under reference of type A can be stored any objects of class A or class that extends A, like in your case B.

So it is possible that

A a = new B();

Since object under a reference is instance of class B it should be possible to somehow store it in more accurate reference, like B b. So lets try this way:

B b = a;//Type mismatch error

Such code gives us compile time Type mismatch error, to prevent this kind of situation:
lets say we have

  • class B1 extends A and
  • class B2 extends A

and we created A a = new B1(). Now if compiler wouldn't throw Type mismatch error for B1 b = a it would should also allow B2 b = a but under a there is instance of B1 which has nothing to do with B2.

To tell compiler that we are aware of potential type mismatch we need to explicitly use casting to our desired class

B b = (B) a;

This is why casting reference A to class that extends A is possible in compile time.


In your code you have

B b1 = (B) new A();
A a1 = (B) new A();

You need to know that new operator is returning reference of the same type that created object, so new A() will return reference of the type A so

B b1 = (B) new A();

is in fact the same as

A tmp = new A();
B b1 = (B) tmp;

Problem here is that you cant store object of some class in reference of its derived class. Why? What if that derived class have new methods that that object class don't have like

class A {
    // some code
}

class B extends A {
    private int i;
    public void setI(int i){
        this.i=i;
    }
}

and after

B b = (B)new A();

you will try to invoke b.setI(42);? Will it be possible? No because instance of class A doesn't have method setI and even if id had, it doesn't have field int i used in this method.

That is why (B)new A(); throws java.lang.ClassCastException.

这篇关于java.lang.ClassException:A不能转换为B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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