【发布时间】:2013-08-25 18:35:10
【问题描述】:
我在获取构造函数中的变量以显示在我的 main 方法的输出中时遇到问题。我可以让程序只使用方法来工作,但是,使用构造函数时会出现问题。任何正确方向的帮助或提示都会很棒!
public class Time {
public static void main (String[] args) {
TimeCalculations time1 = new TimeCalculations();
System.out.println(time1.getCurrentTime());
System.out.println(time1.getElaspedTime());
public static long input() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a time");
return TimeCalculations.elaspedTime = input.nextLong();}
class TimeCalculations {
public long currentTime;
public static long elaspedTime;
public TimeCalculations() {
currentTime = System.currentTimeMillis();
this.currentTime = currentTime;
}
public TimeCalculations(long currentTime, long elaspedTime) {
elaspedTime = currentTime -Time.input();
}
public long getCurrentTime() {
return this.currentTime;
}
public long getElaspedTime() {
return TimeCalculations.elaspedTime;
}
【问题讨论】:
-
你到底有什么问题?
-
我可以让我的 currentTime 变量(以毫秒为单位的时间)显示在我的输出中,但不能显示 elaspedTime 变量。在我的第二个构造函数中,我什至尝试将 elaspedTime 设置为给定值,但仍然没有返回我的输出。我没有包括我的输入法以节省空间。
标签: java variables constructor overloading constructor-overloading