簡體   English   中英

從其他類繼承方法

[英]Inheriting methods from other classes

我正試圖將繼承權分配給uni任務,到目前為止,我遇到了一些問題。

我正在嘗試在我的Pet類中構造一個包含以下代碼的方法:

public class Pet {

    protected String printed;

    public Pet() {

    }

    public String checkFunc(String definitelyPrinted) {
        printed = "CheckFunc is working! Oh boy!";
        System.out.println("Those variables were useless");

        return printed;
    }

}

這由以下人員調用:

public class KennelDemo extends Pet {
private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
private Pet pet; // holds the pet
private Scanner scan; // so we can read from keyboard
private String tempFileName;
private String dogsFile = "dogs.txt";
private String catsFile = "cats.txt";

    private void checkFuncMain() {
        String definitelyPrinted;
        definitelyPrinted = pet.checkFunc(printed);
        System.out.print(definitelyPrinted);
    }
}

然后從控制台菜單中運行:

    case "7":
    checkFuncMain();
    break;

以及此菜單的輸出:

private void runMenu() {
    String response;
    do {
        printMenu();
        System.out.println("What would you like to do:");
        scan = new Scanner(System.in);
        response = scan.nextLine().toUpperCase();
        switch (response) {
        case "1":
            admitDog();
            break;
        case "2":
            changeKennelName();
            break;
        case "3":           
            printDogsWithBones();
            break;
        case "4":
            searchForDog();
            break;
        case "5":
            removeDog();
            break;
        case "6":
            setKennelCapacity();
            break;
        case "7":
            printAll();
            break;
            // TODO
        case "a":
            checkFuncMain();
            break;
        case "Q":
            break;
        default:
            System.out.println("Try again");
        }
    } while (!(response.equals("Q")));
}

是:

Try again.

很簡單,我只是想打印出"CheckFunc is working, oh boy!" 通過繼承,一旦我了解了繼承及其運作方式,就可以完成分配。

目前它沒有運行。 我嘗試了一些不同的操作(例如,將checkFunc String更改為void而不返回任何內容),但我無法弄清楚。

有人可以向我解釋一下嗎?

提前致謝。

當要求用戶輸入某些內容時,您可以

response = scan.nextLine().toUpperCase();

因此,當您輸入字母a ,它不是大寫字母,因此不會接受該大小寫,而只會使用default case

顯然,輸入是問題所在。 您正在從System.in中讀取與所有case都不匹配的內容。

我完全看不到您如何使用繼承。

你的子類是什么? 我也沒有真正看到您如何利用面向對象的編程。

甚至很難說出主要方法中發生了什么,因為您沒有顯示pet的構造。

對於繼承,您可以嘗試如下操作:

class Pet {
    protected String printMsg;

    public Pet() {
        this.printMsg = "I am just a pet, I have nothing interesting to say.";
    }

    public String getPrintMsg() {
        System.out.println("In getPrintMsg() for " + String.valueOf(this.getClass()) + ".");
        return this.printMsg;
    }
}

class Cat extends Pet {
    public Cat() {
        super(); // Call Pet() constructor.
        this.printMsg = "I am a cat, I go meow."; // Cat gets access to printMsg since it is a "protected" property of Pet, which is a parent class to Cat.
    }
}

class Dog extends Pet {
    public Dog() {
        super(); // Call Pet() constructor.
        this.printMsg = "I am a dog, I go woof and bark."; // Cat gets access to printMsg since it is a "protected" property of Pet, which is a parent class to Dog.
    }
}

public class PetApp {
    public static void main(final String[] args) {
        Pet pet = new Pet();
        System.out.println(pet.getPrintMsg());

        pet = new Cat(); // Since Cat extends Pet, I can set pet of Pet type to a new instance of Cat.
        System.out.println(pet.getPrintMsg());

        pet = new Dog(); // Since Dog extends Pet, I can set pet of Pet type to a new instance of Dog.
        System.out.println(pet.getPrintMsg());
    }
}

如果現在運行PetApp,將得到以下輸出:

$ gedit PetApp.java

$ javac PetApp.java 

$ java PetApp
In getPrintMsg() for class Pet.
I am just a pet, I have nothing interesting to say.
In getPrintMsg() for class Cat.
I am a cat, I go meow.
In getPrintMsg() for class Dog.
I am a dog, I go woof and bark.

$

希望能有所幫助。

更進一步,現實世界中的應用程序可能會為Pet使用interfaceabstract class ,因為Pet要么是一種行為模式,要么是一個類,該類具有一些可以執行某些操作的方法,而其他方法則需要通過實現一個兒童班。 您不能創建任何abstract classinstance ,但是可以定義方法,甚至可以在abstract class中使用某些方法的代碼。 作為interface的地方只是實現該接口的任何class都需要遵循的行為模式。

暫無
暫無

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

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