我正在尝试解决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正在工作,哦天哪!”
,一旦我理解了它以及它是如何工作的,我就可以完成我的作业。
目前它还没有运行。我尝试了一些不同的方法(例如将 checkFunc
的 String 更改为 void 并且不返回任何内容),但我无法弄清楚。
有人可以向我解释一下吗?
提前致谢。
最佳答案
当用户被要求输入一些内容时,你会这样做
response = scan.nextLine().toUpperCase();
因此,当您输入字母a
时,它不是大写的,因此它不会接受该大小写,而只会使用默认大小写
.
关于java - 从其他类继承方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710567/