从其他类继承方法

时间:2015-04-17 22:38:12

标签: java inheritance

我正试图围绕继承进行单项任务,到目前为止我遇到了一些问题。

我正在尝试在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!",一旦我理解了它以及它是如何工作的,我就可以完成我的任务。

目前它没有运行。我尝试了一些不同的东西(比如将{String更改为checkFunc的空格而不返回任何内容)但我无法弄明白。

有人可以向我解释一下吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

当要求用户输入内容时,您可以

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

因此,当您输入字母a时,它不是大写字母,因此它不接受这种情况,它只会使用default case

答案 1 :(得分:0)

显然输入是问题所在。您正在阅读System.in中与任何case不匹配的内容。

答案 2 :(得分:0)

我根本不知道你是如何使用继承的。

你的子类是什么?我也没有真正看到你如何利用面向对象的编程。

甚至很难说出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.

$ 

希望有所帮助。

为了更进一步,真实世界的应用程序可能会使用interfaceabstract class来表示Pet,因为Pet可能是行为模式,也可能是具有一些方法的类做一些事情,但需要由子类实施的其他事情。您无法创建任何instance的{​​{1}},但您可以定义方法,甚至可以为abstract class中的某些方法设置代码。其中abstract class只是实现接口的任何interface需要遵循的行为模式。