簡體   English   中英

創建包含超類和子類對象的對象數組的正確方法

[英]Proper way of creating an array of Objects holding objects from superclass and subclass

問題出在我的第二個切換案例1上:我將放置“ arrayEmployees [0]”。 但在超類PersonData或子類personLocation中看不到我的方法。 我對多態性的理解以及內部“對象”的可能性有點陰暗,因為我剛剛開始學習這些東西,所以也許我指稱它們是錯誤的。

我得到了這些指示:

用一個主要方法設計一個名為PersonTest的新類,該類定義一個PersonData對象和一個PersonLocation對象(兩個都沒有參數)以及另外兩個帶有參數的對象,並將所有對象存儲在Array中以檢索和修改實例化對象(即Object Array) )。

我的實際代碼

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;

實際上,這正是您想要做的。 這是一個既包含給定類型又包含其超類的對象的數組。

但是,這樣做會導致丟失一些信息,正如您所注意到的那樣。

創建Object[] ,您要告訴編譯器“這是Object的數組”。 因此,當您去檢索該數組的元素時,編譯器只知道“此數組包含Object ”。 它不知道前兩個元素是PersonLocation實例,也不知道后兩個元素是personData元素。 它只知道該數組包含Object


這是Java集合通常如何工作的基本限制。 Java中的集合具有一種 “總體”類型,因為您將始終具有“ NumberCollection ”,“ PersonData的數組”,“ StringArrayList ”等,而不是“ IntegerCollectionDouble s`“因此,無論內部內容的實際類型是什么,所有編譯器都知道從集合中獲取的對象的類型就是該集合的類型。

例如,說我有List<Number> list = new ArrayList<Number>(); 編譯器只知道內容是Number 它不知道第一個元素是否為IntegerDoubleLong等。它只是一個Number 因此,您不能使用特定於Long方法,如list.get(0).longValue() ,因為編譯器無法保證第一個元素是Integer 它只知道list.get(0)返回一個Number ,並且Number沒有longValue()方法。


那么您如何解決呢?

您有幾種選擇。

  1. 使用instanceof運算符測試arrayEmployees[0]的實際類型,然后根據需要進行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聲明為Integer x = 1;x instanceof Integerx instanceof Number都將返回true Integer x = 1;


希望這足以讓您入門。 隨意問任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM