Java数组中的ArrayStoreException [英] ArrayStoreException in java arrays

查看:76
本文介绍了Java数组中的ArrayStoreException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是《 Core Java》一书中的一个例子

It is an example in the book Core Java

两类员工和经理.经理扩展员工.下面的代码

Two class Employee and Manager. Manager extends Employee. The code below

Manager[] mans ={new Manager("Adam"),new Manager("Ben")};              
Employee[] emps =mans ;   
emps[0] = new Employee ("Charlie");//throws ArrayStoreException in runtime actually;

作者说,在这种情况下,数组应保持相同类型中的元素,否则将引发ArrayStoreException. 在我看来,emps [0]只是对实例'new Employee("Charlie")'的引用,而emps [0]的类型是Employee之前声明的.那么为什么会引发异常.我的基础知识有问题吗?

The author said that in this situation the elements in arrays SHOULD REMAIN THE SAME TYPE or it throws ArrayStoreException. In my mind emps[0] is just a reference to instance 'new Employee ("Charlie")' and type of emps[0] is Employee declared before.So why it throws exception.Is there something wrong with my basics?

推荐答案

创建数组时,它会记住要存储的数据类型.因此,如果您有课程

When array is created it remembers what type of data it is meant to store. So if you have classes

class Employee { .. }
class Manager extends Employee { .. }

您将创建数组

Manager[] arrM = new Manager[10];

数组会记住,它只需要存储Manager类或其派生类型的实例.但是此类数组无法存储Manager的超级类型,因为超级类型可能不具有Manager类的所有方法或字段,因此类似

array will remember that it need to store only instances of Manager class or its derived types. But such array can't store super type of Manager since super type may not have all methods or fields of Manager class so something like

arrM[0] = new Manager();

可以,但是

arrM[0] = new Employee();

抛出java.lang.ArrayStoreException: Employee表示此处的Employee不是正确的参数.

throws java.lang.ArrayStoreException: Employee pointing that Employee is not correct argument here.

所以您的情况就是

  • 为经理创建数组

  • creating array for Managers

Manager[] mans ={new Manager("Adam"),new Manager("Ben")};

  • 使用其超类型Employee

    Employee[] emps =mans;
    

    (但是此引用仍然指向只能容纳向导"的数组)

    (but this reference it still pointing to array which can hold only Menagers)

    并尝试放入数组new Employee

    emps[0] = new Employee("Charlie");
    

  • 但是正如我提到的那样,这是不允许的,因为Employee可能没有与Manager相同的成员.假设Menager可以hire(...)某人,而Employee不能(没有此方法).如果您打电话给

    but as I mentioned this can't be allowed because it is possible that Employee do not have same members as Manager. Lets say Menager can hire(...) someone, while Employee can't (doesn't have this method). What would happen if you would call

        mans[0].hire(new Employee("Tom");
    

    emps[0] = new Employee ("Charlie");不会引发异常,但可以让Employee放在emps[0]中吗?由于mansemps使用相同的数组,这意味着将从Employee("Charlie")调用mans[0].hire(new Employee("Tom"),因为雇员没有hire方法,因此无法雇用任何人.

    and emps[0] = new Employee ("Charlie"); would not throw exception but would let you place Employee in emps[0]? Since mans and emps are using same array it would mean that mans[0].hire(new Employee("Tom") would be called from Employee("Charlie") which can't hire anyone because Employee don't have hire method.

    这就是为什么您不能在管理器数组中放置超类型(雇员)实例的原因.

    That is why you can't place instance of super-type (Employee) in array of Managers.

    这篇关于Java数组中的ArrayStoreException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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