[英]JAVA: How to call a method in a child class of an Object?
我正在嘗試解決此問題,但實際上我不理解語法。 考慮以下代碼:
class Parent {
private String productCode1; // Format X9999
private String productCode2;
public Parent(String productCode1, String productCode2)
{
this.productCode1= productCode1;
this.productCode2= productCode2;
}
}
然后我有一個子類,其中包含我要調用的方法
public Child extends Parent{
private String[] serialNumbers;
public Child(String productCode1, String productCode2, String... args){
super(productCode1, productCode2,);
int i = 0;
for(String serialN: args){
serialNumbers = new String[10];
serialNumbers[i]= serialN;
i++;
}
public String[] getSerialNumbers(){ //This is the method i want to call
return this.serialNumbers;
}
}
這是庫存類
public class Inventory{
public ArrayList<Parent> products;
public Inventory(){
products = new ArrayList<Parent>(); //Some hard coded items
products.add(new Parent("A0001", "abcdefg"));
products.add(new Parent("B0010", "abcdefg"));
products.add(new Parent("C0100", "abcdefg"));
products.add(new Child("D1000", "abcdefg", "firstSN", "secondSN", "thirdSN"));
}
public static ProductLine getProduct(ArrayList<Parent> products, String productCode){
for(int i = 0; i < products.size(); i++){
ProductLine product = products.get(i);
if(product.getProductCode().equals(productCode)){
return product;
}
}
return null;
}
現在在主類中,我需要執行檢查對象是父對象還是子對象的操作,以嘗試獲取序列號,如果我得到一個序列號,則意味着是孩子,否則是父母。
我正在嘗試使用子類中的getserialNumbers()的唯一問題
public Application{
String inputProductCode = keyboard.nextLine(); //Input product code from user
Inventory inventory = new Inventory();
ProductLine pFound = inventory.getProduct(inventory.products, inputProductCode); //check if is Child item
String[] serNumb = pFound.getSerialNumbers(); //Here is where i get Error cannot find symbol/method getSerialNumbers
if(serNumb[0] == null){
// code continue...
}
我試圖投像:
String[] serNumb = ((ItemisedProductLine)pFound).getSerialNumbers();
它可以編譯,但是他在運行時向我拋出(如預期的那樣)“無法將父對象強制轉換為子對象”。
我希望我對這個長期的問題深表歉意...
對。 您不能做演員,因為Parent
根本不是Child
。
您可以使用instanceof
來檢查您擁有的對象類型,然后僅在具有Child
實例的情況下才執行強制轉換和調用,但通常希望避免執行此類操作。 OOP不好。
您可以做的一件事是將getSerialNumbers()
方法放在Parent
,並始終返回一個空數組,然后在Child
對其進行覆蓋以實際返回Child
的序列號。
當然,這里值得重新考慮整個類的層次結構。 對於從Parent
繼承的所有類都具有序列號是否有意義? 如果是這樣,則應考慮將所有序列號處理移至Parent
。 Parent
子類別會比Child
嗎? 如果沒有,也許應該只是消除Child
。
這是OOP
編程模型中的簡單問題:
Child
實例也都是Parent
實例。 Parent
實例也可以是Child
實例。 如果按以下方式更改Inventory
類構造函數,則程序可以正常工作:
public Inventory()
{
products = new ArrayList<Parent>(); //Some hard coded items
products.add(new Child("A0001", "abcdefg"));
products.add(new Child("B0010", "abcdefg"));
products.add(new Child("C0100", "abcdefg"));
products.add(new Child("D1000", "abcdefg", "firstSN", "secondSN", "thirdSN"));
}
更好的方法是在將Parent
實例強制轉換為Child
之前,通過instanceof
關鍵字進行檢查。
為什么Parent類沒有getSerialNumbers方法? 我認為您有兩個選擇:在Parent類中實現,或者在您的邏輯中不考慮Parents(用instanceof測試)。
除非並且直到它是父類中定義的抽象方法,否則您不能調用子類的方法。 因為JVM不了解子類中具有的方法。 為此,您可以在Inventory
類中執行以下操作。
public class Inventory{
public ArrayList<Parent> products;
public Inventory(){
products = new ArrayList<Parent>(); //Some hard coded items
products.add(new Child("A0001", "abcdefg"));
products.add(new Child("B0010", "abcdefg"));
products.add(new Child("C0100", "abcdefg"));
products.add(new Child("D1000", "abcdefg", "firstSN", "secondSN", "thirdSN"));
}
public static ProductLine getProduct(ArrayList<Parent> products, String productCode){
for(int i = 0; i < products.size(); i++){
ProductLine product = products.get(i);
if(product.getProductCode().equals(productCode)){
return product;
}
}
return null;
}
如果您的目標是確定對象是父類型還是子類型,則可以使用instanceof關鍵字來實現:
if(pFound instanceof Child) {
//...
}
如果要使用序列號區分類型,則Parent類也必須強制使用getSerialNumber方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.