[英]Accessing Scanner objects from subclass in a superclass?
我是一個非常新的,相對缺乏經驗的 Java 程序員。 我正在做的項目只是對我當前技能的測試,我的目標是編寫盡可能高效的程序。
本質上,我有三個類:A、B 和 C。 B 擴展 A 和 C 擴展 B,但我希望 C 中的掃描儀 object 用於 A 中的 switch 語句(更大方法的一部分)。
我想要這個的原因是因為我不想重載A中的方法(復制粘貼具有不同參數的相同代碼並不理想),並且我不想將所有類合並為一個(代碼很簡單足以做到這一點,但我想測試我對 object 創建和使用的知識)。
這是一些代碼:
import java.time.LocalDateTime;
public class WatchFace {
// MASTER TIME
LocalDateTime dateTimeObject = LocalDateTime.now();
int hour = dateTimeObject.getHour();
int minute = dateTimeObject.getMinute();
// WATCH FACE METHOD
public void watchFaceMethod() {
// Code I'd like to utilize; this is my question for StackOverflow
// switch (userInput) {
// case 1:
// // Intentionally do nothing
// break;
//
// case 2:
// // Change minute and hour to some values obtained by timezone stuff
// break;
//
// case 3:
// // Change both minute and hour to -1
// break;
// }
// Basically, the rest of this code just prints something different to the Windows CLI depending on the
// hour and minute variables' current values (i.e. beyond the intended switch statement).
}
}
import java.time.format.DateTimeFormatter;
public class Watch extends WatchFace {
static void watchMethod() {
// Code printing some Strings is here.
WatchFace watchFaceObject = new WatchFace();
watchFaceObject.watchFaceMethod();
// Code printing some more Strings is here.
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
String dateTimeDisplay = watchFaceObject.dateTimeObject.format(dateTimeFormat);
System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
if (watchFaceObject.hour == 11 && watchFaceObject.minute == 11) {
System.out.println("Make a wish!");
}
}
}
import java.util.Scanner;
public class InteractiveWatch extends Watch {
public static void main(String[] args) {
// WATCH OBJECT
Watch watchObject = new Watch();
// STARTUP
System.out.println("Welcome to Interactive Watch!\n");
System.out.println("What would you like to do?");
System.out.println("[1] See local time.");
System.out.println("[2] See local time in a particular place.");
System.out.println("[3] See something special.\n");
Scanner scannerObject = new Scanner(System.in);
// INPUT
boolean loopBreak = true;
while (loopBreak) {
loopBreak = false; // loopBreak set to false
String userInput = scannerObject.nextLine(); // User inputs some string
switch(userInput) {
case "1":
watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 1
break;
case "2":
watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 2
break;
case "3":
watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 3
break;
default:
loopBreak = true; // loopBreak set to true; while loop reinitiates
System.out.println("\nPlease enter a valid key.\n");
break;
}
}
}
}
我從 w3schools 的 Java 課程中學到了一切,但我還有很多東西要學。 讓我知道我想要的是否可能,或者其他任何可以使這段代碼更有效率的東西。 謝謝!
不,這是不可能的。
超類無法訪問 Java 中其子類的成員。 但是子類可以訪問其超類的所有非私有成員。
簡短的回答是否定的。 您無法訪問屬於某個子類型的 object。
長答案: watchFaceMethod
不知道調用來自InteractiveWatch
。 這樣想; 如果我們制作一個新的 class OtherWatch
會怎樣,它也擴展了Watch
。 假設OtherWatch
沒有掃描器 object。 現在,當watchFaceMethod()
被告知調用掃描儀 object 的方法時,它會做什么? 它什么也做不了,因為掃描儀 object 不存在。 不過,我不確定我為什么要首先嘗試在watchFaceMethod
中訪問掃描儀 object。 你已經得到了用戶的輸入。 您不想獲得更多輸入,因此您確實希望訪問nextLine()
方法返回的字符串。 我將通過簡單地將字符串向上傳遞作為watchMethod()
和watchFaceMethod()
方法的參數來解決此問題。 將參數傳遞給另一個方法並不是“低效的”。 你最終會得到類似這樣的方法:
public void watchMethod(String userInput) {
...
WatchFace watchFaceObject = new WatchFace();
watchFaceObject.watchFaceMethod(userInput);
...
}
和
public void watchFaceMethod(String userInput) {
switch (userInput) {
case "1":
...
break;
case "2":
...
break;
case "3":
...
break;
}
...
}
另一種選擇是將userInput
公開的 static 變量,然后僅從watchFaceMethod()
引用它,但我建議不要這樣做,因為您可能很快就會忘記哪些方法正在訪問和改變該變量。
關於您的代碼,我還注意到了一件小事; 您使用\n
作為行分隔符,這會產生換行符。 這是 UNIX 系統上的標准行分隔符,但是 Windows 使用回車符和換行符,而 OSX 只使用回車符,因此如果您希望您的回車出現在所有平台上,您應該使用%n
,它會產生正確的特定於平台的行分隔符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.