gpt4 book ai didi

java - 创建保存父类(super class)和子类对象的对象数组的正确方法

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:55 25 4
gpt4 key购买 nike

问题出在我的第二个开关案例 1 上:我将放置“arrayEmployees[0]”。但它在父类(super class) PersonData 或子类 personLocation 中看不到我的方法。我对多态性以及内部“对象”可能性的理解有点可疑,因为我刚刚开始学习这些,所以也许我引用它们是错误的。

我得到了以下指示:

Design a new class called PersonTest with a main method that defines a PersonData object and a PersonLocation object (both without arguments) and two more objects with arguments and store all the objects in an Array for retrieval and modification of instantiated objects (i.e. Array of Objects).

我的实际代码

package lab5;
import java.util.InputMismatchException;
import java.util.Scanner;

public class PersonTest
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);

PersonLocation personLocation = new PersonLocation();
PersonData personData = new PersonData();

PersonLocation personLocationOverLoaded = new PersonLocation("Hamilton");
PersonData personDataOverloaded = new PersonData("Stirling", "905-567-7656");

Object[] arrayEmployees = new Object[4];
arrayEmployees[0] = personLocation;
arrayEmployees[1] = personLocationOverLoaded;
arrayEmployees[2] = personData;
arrayEmployees[3] = personDataOverloaded;

int user = 0;
int menu = 0;

// Get input here, put into variable "user"

switch(user)
{
case 1:
System.out.print("Printing Object Information With Given Values\n\n\t");

arrayEmployees[0]. //Issue
}
}//End Main Method
}//End Class PersonTest

应该发生什么:我应该能够从我的数组中进行引用,如上所示(arrayEmployees[0]。),并为该特定类显示我的方法。

最佳答案

Object[] arrayEmployees = new Object[4];
arrayEmployees[0] = personLocation;
arrayEmployees[1] = personLocationOverLoaded;
arrayEmployees[2] = personData;
arrayEmployees[3] = personDataOverloaded;

实际上,这完全完成了您想做的事情。这是一个包含给定类型及其父类(super class)对象的数组。

但是,正如您所注意到的,这样做会丢失一些信息。

当您创建Object[]时,您告诉编译器“这是一个 Object 的数组”。因此,当您检索该数组的元素时,编译器只知道“该数组包含 Object s”。它不知道前两个元素是 PersonLocation实例,并且不知道最后两个元素是 personData元素。它只知道数组包含 Object s。

<小时/>

这是 Java 中集合一般工作方式的基本限制。 Java 中的集合有一个“整体”类型,就像你总是有“Collection of Number s”、“array of PersonData s”、“ArrayList of String s"等,而不是 "Collection of Integer s 和 Double s`"因此,无论内部内容的实际类型,所有编译器都知道您将从集合中获取的对象的类型就是该集合的类型。

例如,假设我有 List<Number> list = new ArrayList<Number>(); 。编译器只知道内容是 Number s。它不知道第一个元素是否是 Integer , Double , Long等等,这只是一个 Number 。因此,您不能使用 Long -具体方法如list.get(0).longValue() ,因为编译器无法保证第一个元素是 Integer 。它只知道list.get(0)返回 Number ,以及Number没有 longValue()方法。

<小时/>

那么如何解决这个问题呢?

您有几个选择。

  1. 使用instanceof运算符(operator)进行测试arrayEmployees[0]其实际类型,然后根据需要进行强制转换并执行所需的方法。这比较尴尬,但如果您必须有一个数组,那么您实际上没有太多选择。
  2. 为每个类使用单独的数组,因此不会丢失任何信息。

考虑到您需要执行的分配,似乎多个数组不是一个选项,因为指令指定了单个数组,因此您可能需要进行测试和转换。总体思路如下:

<variable> instanceof <type>

测试是否 variable是-a <type> 。例如:

arrayEmployees[0] instanceof PersonLocation

测试是否 arrayEmployees[0]PersonLocation .

此测试返回 boolean ,因此您可以将其用作 if 的条件陈述。里面if声明,你可以垂头丧气arrayEmployees[0]进入临时引用:

if (arrayEmployees[0] instanceof PersonLocation) {
PersonLocation temp = (PersonLocation) arrayEmployees[0];
// execute whatever you need on temp
}

请注意 -- instanceOf也将匹配子类。所以两者x instanceof Integerx instanceof Number都会返回 true如果x声明为Integer x = 1; .

<小时/>

希望这足以让您开始。如有任何问题,请随时提出。

关于java - 创建保存父类(super class)和子类对象的对象数组的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com