簡體   English   中英

從超類調用子類方法

[英]Calling a subclass method from superclass

我在介紹性的 java 課程中,我們剛剛開始學習 inheritance。我正在進行一項任務,要求我們創建一個具有名稱和年齡的“寵物”超類; 和三個子類,每個子類都有自己獨特的特征(我選擇了“狗”、“貓”和“鳥”)。 在我們完成所有這些構建之后,我們將創建一個 Main class 來測試所有內容,這就是我遇到問題的地方。 我試圖為Main中的這些獨特特征調用get方法,但它似乎只能找到超類中的方法。

這是主要的 class:

public class Kennel {
    public static void main(String[] args) {
        // Create the pet objects
        Pet cat = new Cat("Feline", 12, "Orange");
        Pet dog = new Dog("Spot", 14, "Dalmation");
        Pet bird = new Bird("Feathers", 56, 12);

        // Print out the status of the animals
        System.out.println("I have a cat named " + cat.getName()
                + ". He is " + cat.getAge() + " years old."
                + " He is " + cat.getColor()
                + "When he speaks he says " + cat.speak());
        System.out.println("I also have a dog named " + dog.getName()
                + ". He is " + dog.getAge() + " years old."
                + " He is a " + dog.getBreed()
                + " When he speaks he says " + dog.speak());
        System.out.println("And Finally I have a bird named " 
                + bird.getName() + ". He is " + bird.getAge() + " years old."
                + " He has a wingspan of " + bird.getWingspan() + " inches."
                + " When he speaks he says " + bird.speak());       
    }
}

這是我的超類

abstract public class Pet {
    private String name;
    private int age;

    // Constructor
    public Pet(String petName, int petAge) {
        this.name = petName;
        this.age = petAge;
    }

    // Getters
    public String getName() { return(this.name); }
    public int getAge() { return(this.age); }

    // Setters
    public void setName(String nameSet) { this.name = nameSet; }
    public void setAge(int ageSet) { this.age = ageSet; }

    // Other Methods
    abstract public String speak();

    // toString
    @Override
    public String toString() {
        String answer = "Name: " + this.name + " Age: " + this.age;
        return answer;
    }
}

這是其中一個子類(它們看起來都一樣並且有相同的錯誤)

public class Cat extends Pet {
    private String color;

    // Constructor
    public Cat(String petName, int petAge, String petColor) {
        super(petName, petAge);
        this.color = petColor;
    }

    // Getters
    public String getColor() { return(this.color); }

    // Setters
    public void setColor(String colorSet) { this.color = colorSet; }

    // Other Methods
    @Override
    public String speak() { return "Meow!"; } 

    // toString
    @Override
    public String toString() {
        String answer = "Name: " + super.getName() + " Age: "+super.getAge()
                + " Color: " + this.color;
        return answer;
    }
}

所以發生的事情是我無法找到 main 方法來找到cat.getColor()方法,或者子類獨有的任何其他方法。

當您將變量聲明為具有超類的類型時,您只能通過該變量訪問超類的(公共)方法和成員變量。

Pet cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet

要訪問具體 class(在本例中為Cat )中的方法,您需要將變量聲明為派生的 class

Cat cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // OK, getName() is part of Cat (and the superclass)
cat.getColor(); // OK, getColor() is part of Cat

或者將其轉換為您知道/懷疑是具體類型的類型

Pet cat = new Cat("Feline",12,"Orange"); 
((Cat)cat).getName(); // OK (same as above)
((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat

您甚至可以結合這兩種方法:

Pet pet = new Cat("Feline",12,"Orange"); 
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK
Pet cat = new Cat("Feline",12,"Orange");
^^^
This is the error.

Pet沒有名為getColor()的方法

你需要做:

Cat cat = new Cat(...);

當你這樣做時:

 Pet cat = new Cat("Feline",12,"Orange");

編譯器將變量 cat 視為 Pet。 所以不能使用Cat類的具體方法。

您必須將 cat 聲明為 Type Cat 才能解決您的問題。

問候。

在這里,您嘗試使用作為子類方法的構造函數 (Cat) 創建超類類型對象 (cat),這是不可能的。 這違反了 Inheritance 的規則。

Pet cat = new Cat("Feline", 12, "Orange");

您還可以將這些空方法添加到您的 Pet class 中:

public String getColor() { }
public String setColor() { }
public String speak() { }

這樣您就可以從 Pet object 調用此方法,而不必將其強制轉換為 Cat。 一旦它被調用,Cat 的方法就會覆蓋超類中的一個方法。 這個技巧的缺點是您必須在超類中為您打算從子類調用的每個方法聲明一個空方法,如果要添加多個方法,這可能會變得混亂。 因此,只對將在許多不同子類中使用的方法執行此操作。

暫無
暫無

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

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