从其他 类 继承方法

Inheriting methods from other classes

我正在尝试围绕 uni 作业的继承进行思考,到目前为止我已经 运行 遇到了一些问题。

我正在尝试在我的 Pet class 中构建一个方法,其中包含以下代码:

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 不匹配的内容。

我完全不明白你是如何使用继承的。

你的子class是什么?我也不太明白您如何利用面向 object 的编程。

甚至很难说出您的 main 方法中发生了什么,因为您没有显示 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 要么是一种行为模式,要么是 class有一些方法可以做一些事情,但其他方法需要 child class 来实现。您不能创建任何 abstract classinstance,但您可以定义方法,甚至可以在 abstract class 中包含某些方法的代码。 interface 只是任何实现接口的 class 需要遵循的行为模式。