[英]Java: calling a super class' protected method from a subclass - not visible?
[英]Calling subclass method from super class
只是想知道我是否能得到一些幫助。
我正在嘗試調用以下方法:
public void updateBrand(Scanner input){
System.out.println("1 - HIGHSTREET\n2 - FRENCHCONNECTION\n3 - TEDBAKER\nSelect type of brand (Enter number)");
int choice = input.nextInt();
switch (choice) {
case (1):
setBrand(Brand.highstreet);
System.out.println("Brand of has been changed to HIGHSTREET");
break;
case (2):
setBrand(Brand.frenchconnection);
System.out.println("Brand of has been changed to FRENCHCONNECTION");
break;
case (3):
setBrand(Brand.tedbaker);
System.out.println("Brand of has been changed to TEDBAKER");
break;
default: System.out.println("Invalid input");
break;
}
}
從我的MorningSuit子類到我的Suit Super類中的方法:
public void makeChange(Scanner input){
System.out.println("Are you sure you want to change this suit? (y or n)");
String choice;
choice = input.next();
if (choice.toLowerCase() == "y") {
updateBrand(input);
}
else if (choice.toLowerCase() == "n") {
System.exit(0);
}
else {
System.out.println("Invalid Input");
}
}
但是當我嘗試在我的超級類中調用updateBrand(input)方法時收到錯誤,因為它不存在。 我該如何解決?
您不能在超類的子類中調用方法。
畢竟,如果實例實際上是另一個子類,該怎么辦?
您應該將這兩個方法都移到同一類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.