我是(非常)业余Java程序员,遇到了一些麻烦。本质上,我有2个类:A和B,其中B扩展了A。
在A中,我定义了一个创建一些ZonedDateTime对象的方法。我知道如何在B中创建A的实例,以便可以在B中执行A的方法,但是问题出在稍后需要直接访问该方法的ZonedDateTime对象时。我的第一个猜测是像这样访问它们:
// Assuming the ZonedDateTime objects, e.g. dateTimeA, are within ClassA's "methodA()" method...
ClassA objectA = new ClassA();
System.out.println(objectA.methodA().dateTimeA);
当这不可避免地出错时,我想我需要返回的方法,但是据我所知,这是不可能的。您只能返回某些数据类型,而不能返回对象。我还应该提到,我需要将这些对象保留为对象,因此将它们转换为String然后以这种方式返回它们对我来说是行不通的。
这个问题感觉很基础,但是即使如此,我也找不到其他答案。很抱歉,如果这是一个重复的问题。任何帮助将不胜感激。
修改(S):
这里有更多代码可以使问题更容易重现:
WatchFaceEDIT(即A):
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class WatchFaceEDIT {
// Up here are some variable declarations and things used in the switch statement below.
public void watchFaceMethod(String userInput) { // userInput is a parameter passed up from a subclass of this class.
switch (userInput) {
case "1":
ZonedDateTime dateTimeHere = ZonedDateTime.now();
hour = dateTimeHere.getHour();
minute = dateTimeHere.getMinute();
amPM = dateTimeHere.getHour();
break;
case "2":
ZonedDateTime dateTimeThere = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
hour = dateTimeThere.getHour();
minute = dateTimeThere.getMinute();
amPM = dateTimeThere.getHour();
break;
case "3":
hour = -1;
minute = -1;
break;
}
// The rest of the code in WatchFaceEDIT does some things with these hour and minute variables.
}
}
WatchEDIT(即B):
import java.time.format.DateTimeFormatter;
public class WatchEDIT extends WatchFaceEDIT {
static void watchMethod(String userInput) {
WatchFaceEDIT watchFaceObject = new WatchFaceEDIT();
watchFaceObject.watchFaceMethod(userInput);
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
String dateTimeDisplay = watchFaceObject.watchFaceMethod(userInput).dateTimeHere.format(dateTimeFormat);
// There is more to the code than this, but the problem seems to be here.
}
}
在这里我将简短地提到,我认为问题可能与switch语句的范围有关,但是在WatchEDIT中,我能够毫无问题地运行watchFaceMethod,并且该方法确实利用了创建的对象来显示事物。 / p>
答案 0 :(得分:2)
我检查了您的初始post,以了解更多相关信息,并建议采用更合适的重构方式:
static
使用watchMethod
-这样您就可以引用其超类的成员WatchFaceEDIT
中的打印功能以分隔方法WatchFaceEDIT::watchFaceMethod
中设置所有相关值话虽如此,代码修复如下:
public class WatchFaceEDIT {
// instance variables set inside watchFaceMethod
protected int hour;
protected int minute;
protected int amPM;
protected ZonedDateTime dateTimeObject;
public void watchFaceMethod(String userInput) {
switch (userInput) {
case "1":
dateTimeObject = ZonedDateTime.now();
hour = dateTimeHere.getHour();
minute = dateTimeHere.getMinute();
amPM = dateTimeHere.getHour();
break;
case "2":
dateTimeObject = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
hour = dateTimeThere.getHour();
minute = dateTimeThere.getMinute();
amPM = dateTimeThere.getHour();
break;
case "3":
dateTimeObject = null;
hour = -1;
minute = -1;
amPM = -1;
break;
}
printSomething();
}
public void printSomething() {
// move here the printing code from watchFaceMethod
// if needed you may override this method in WatchEDIT class to modify display
// The rest of the code in WatchFaceEDIT does some things with these hour and minute variables.
}
}
public class WatchEDIT extends WatchFaceEDIT {
public void watchMethod(String userInput) {
// call method defined in super class and set instance variables
watchFaceMethod(userInput);
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
String dateTimeDisplay = dateTimeObject == null ? "N/A" : dateTimeObject.format(dateTimeFormat);
// There is more to the code than this, but the problem seems to be here.
}
}
仅创建子类实例的主类
// does not need to extend WatchEDIT/WatchFaceEDIT classes
public class Main {
public static void main(String[] args) {
WatchEDIT watchObject = new WatchEDIT();
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);
boolean loopBreak = true;
while (loopBreak) {
loopBreak = false; // loopBreak set to false
String userInput = scannerObject.nextLine(); // User inputs some string
switch(userInput) {
case "1":
case "2":
case "3":
watchObject.watchMethod(userInput);
break;
default:
loopBreak = true; // loopBreak set to true; while loop reinitiates
System.out.println("\nPlease enter a valid key.\n");
break;
}
}
}
}