2012-04-05 158 views
17

我在介紹性的java課程,我們剛開始學習繼承。我正在研究一項任務,要求我們創建一個名稱和年齡爲「Pet」的超類;和三個亞類,每個亞類都有自己獨特的特徵(我選擇了「狗」,「貓」和「鳥」)。在完成所有這些構建之後,我們將創建一個Main類來測試所有內容,這就是我遇到問題的地方。我試圖在Main內調用get這些獨特特徵的方法,但它似乎只能找到超類中的方法。從超類調用子類方法

這裏是主類:

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; 
    } 
} 

所以發生了什麼是我不能主要的方法找到cat.getColor()方法od或任何其他子類所特有的。

+0

什麼是錯誤?我相信,自從你做了'貓貓=新貓(...'而不是'貓貓=新貓(...'。 – twain249 2012-04-05 01:46:58

+0

這清除了它,謝謝! – salxander 2012-04-05 01:48:22

回答

24

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

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

訪問(在這種情況下Cat)在具體的類中的方法,你需要或者變量聲明爲派生類

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 
+3

刪除了我的,因爲這是更完整和完全正確的版本。 – JamesSwift 2012-04-05 01:56:54

+0

這救了我的命 – 2017-05-24 19:12:11

1
Pet cat = new Cat("Feline",12,"Orange"); 
^^^ 
This is the error. 

Pet沒有一個叫getColor()

你需要做的方法:

Cat cat = new Cat(...); 
1

當你這樣做:

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

編譯器看到變量像貓一樣寵物。所以你不能使用Cat類的具體方法。

您必須將cat聲明爲Type Cat來解決您的問題。

問候。

1

這裏您試圖使用作爲子類方法的構造函數(Cat)創建超類型對象(cat),這是不可能的。這違背了繼承的規則。

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