简体   繁体   English

将基础类对象转换为扩展基础类的类时的ClassCastException

[英]ClassCastException when casting a base class object to an class that extends base class

I have mentioned comments along the code line , that I want to understand ie as follows: 我已经在代码行中提到了我想理解的注释 ,如下所示:

  1. I didnt't understand this line of code : Xyz obj3=(Xyz) obj1; 我不明白这行代码:Xyz obj3 Xyz obj3=(Xyz) obj1;

  2. Abc cannot be cast to Xyz type . Abc不能XyzXyz类型。 Compiled , but returns exception during execution : Xyz obj2=(Xyz)new Abc(); 已编译,但在执行过程中返回异常: Xyz obj2=(Xyz)new Abc();

     class Abc { Abc() { System.out.println(" Hello "); } void show() { System.out.println("Conco constructor w/ Super Class"); } } class Xyz extends Abc { Xyz() { System.out.println(" Hi "); } void display() { System.out.println("ConsNew3 constructor w/ Extends Class Conco"); } public static void main(String arg[]) { Xyz obj=new Xyz(); Abc obj1=new Xyz(); obj1.show(); Xyz obj3=(Xyz) obj1; // I didnt't understand this line of code /*Xyz obj2=(Xyz)new Abc(); Abc cannot be cast to Xyz type . Compiled , but returns exception during execution */ } } 

In general humans are not very good in abstract notation, although programs require that. 一般而言,尽管程序要求,人类的抽象符号表示不是很好。

A beginner (more correctly any person) in programming should not use abstract names like Abc, and Xyz, this even confuses advanced programmers. 编程的初学者(更正确地说是任何人)都不应使用Abc和Xyz等抽象名称,这甚至会使高级程序员感到困惑。

Why not just rename Abc to Animal, and Xyz To Dog. 为什么不将Abc重命名为Animal,将Xyz重命名为Dog。

Then you have: 然后您有:

Dog obj3=(Dog) obj1; // works because Dog is an Animal.
Dog obj2=(Dog) new Animal(); // does not compile because not all Animals are Dogs

Xyz obj3=(Xyz) obj1; this line works because Xyz is an Abc . 这行有效,因为XyzAbc

Xyz obj2=(Xyz)new Abc(); this line does not work because not all Abc s are Xyz s 这行不起作用,因为并非所有Abc都是Xyz

As the comments mention it can make more sense to name things this way. 正如评论所提到的,用这种方式命名事物可能更有意义。

Abc == Animal Abc ==动物

Xyz == Dog Xyz ==狗

Dog obj3 = (Dog) obj1;

obj1 was previously instantiated as a Dog although it is being held by a more generic Animal object. obj1以前被实例化为Dog尽管它由更通用的Animal对象持有。 It still is an instance of a Dog so it can be cast back to a Dog . 它仍然是Dog一个实例,因此可以将其转换回Dog

Dog obj2 = (Dog) new Animal();

Animal s are not necessarily Dog s. Animal不一定是Dog

In order to avoid problems like this you can use the instanceof operator. 为了避免出现此类问题,您可以使用instanceof运算符。 This tests whether a generic Object is an Instance of another, usually more specific Object. 这将测试通用对象是否是另一个(通常是更特定的)对象的实例。

You can use this as follows. 您可以如下使用。

Object myGenericDog = new Dog();
Animal myAnimal = new Animal();

if(myGenericDog instanceof Animal){
    myAnimal = (Animal) myGenericDog;
}

For future reference, in cases of both real and example code it is far more beneficial to the developer writing the code and any future developers who may end up maintaining the code to use descriptive class names. 供将来参考,在真实代码和示例代码的情况下,这对开发人员编写代码以及将来可能最终维护代码以使用描述性类名的任何将来的开发人员都有利。 Note: Xyz and Abc are not descriptive . 注意: XyzAbc不是描述性的 They introduce unnecessary complexity. 它们引入了不必要的复杂性。 You can improve your own code and examples by choosing names that are nouns and that describe the responsibility of your classes. 您可以通过选择名词和描述类责任的名称来改进自己的代码和示例。

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

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