我是一个非常新的,相对缺乏经验的Java程序员。我正在从事的项目只是对我当前技能的考验,我的目标是编写尽可能高效的程序。
本质上,我具有三个类:A,B和C。B扩展了A,C扩展了B,但是我希望C中的Scanner对象用于A中的switch语句(较大方法的一部分)
我想要这样做的原因是因为我不想重载A中的方法(用不同的参数复制粘贴相同的代码是不理想的),并且我不想将所有类都合并为一个(代码很简单,但是我想测试一下我对对象创建和使用的了解。
下面是一些代码:
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课程中学到了所有东西,但是我还有很多东西要学习。让我知道我想要的是什至是可能的,或者是任何其他可以使此代码更高效的东西。谢谢!
答案 0 :(得分:0)
不,这不可能。
超类无法访问Java中其子类的成员。但是子类可以访问其超类的所有非私有成员。
答案 1 :(得分:0)
简短的回答是否。您不能访问属于子类型的对象。
长答案:while(processes.size() < 6){
int id = (int)(Math.random() * 10);
int priority = (int)(Math.random() * 10);
int burst_time = (int)((Math.random() * 80) + 20);
Process p = new Process(id, priority, burst_time);
boolean dup = false;
for (int i = 0; !dup && i < processes.size(); i++){
dup = p.getId() == processes.get(i).getId();
}
if (!dup) processes.add(p);
}
不知道呼叫来自watchFaceMethod
。这样想吧;如果我们创建了一个新类InteractiveWatch
,又扩展了OtherWatch
,该怎么办?假设Watch
没有Scanner对象。现在,OtherWatch
在被告知调用Scanner对象的方法时会做什么?它无能为力,因为Scanner对象不存在。不过,我不确定我是否理解您为什么首先尝试访问watchFaceMethod()
中的Scanner对象。您已经从用户那里得到了输入。您不想获得更多输入,因此您确实希望访问watchFaceMethod
方法返回的字符串。我将通过简单地将字符串作为参数传递给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
进行引用,但是我建议您不要这样做,因为您可能会很快失去对访问和变异方法的了解该变量。
我注意到有关您的代码的另一件事;您将watchFaceMethod()
用作换行符,这将产生换行符。这是UNIX系统上的标准行分隔符,但是Windows使用回车符和换行符,而OSX仅使用回车符,因此,如果希望在所有平台上都显示回车符,则应使用\n
,可以生成正确的平台特定的行分隔符。