【问题标题】:Calling variables from overloaded constructors从重载的构造函数中调用变量
【发布时间】: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


【解决方案1】:

对不起!但我没有得到这个! “但是,使用构造函数时会出现问题
您能否提供更多详细信息!
谢谢!
谢谢!为您的详细信息。 只需在第一行调用 public static void main 中的输入法

import java.util.*;
public class Time{
public static void main(String[] args) {
    input();
    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 long getCurrentTime(){
    return this.currentTime;
}

public long getElaspedTime() {      
    return TimeCalculations.elaspedTime;
}

}

【讨论】:

  • 为什么使用 elapsedTime 作为静态变量?
  • 是的。我制作了相同的程序,但使用带有 return 语句的常规方法。从那里,我能够将每个值输入到我的输出中。当我将方法更改为构造函数时,我无法将值传递给另一个类。这有帮助吗?
  • 当我将其更改为非静态时,我收到编译错误“无法对非静态字段进行静态引用”。在我的第二个构造函数中。
  • 你可以在这里发布 Time.input 吗?你在输入法里干什么?
  • 我无法在评论框中正确格式化它,所以我将它添加到我的主代码中。
【解决方案2】:

在您的第二个构造函数中,您基本上没有做任何事情。由于您使用与参数相同的变量名称,因此您的全局变量 elaspedTimecurrentTime 被隐藏。将其更改为以下内容:(添加this.

public TimeCalculations(long currentTime, long elaspedTime) {
    this.elaspedTime = currentTime -Time.input();
}

其次,你在这个构造函数中所做的事情是没有意义的。在这种情况下,将 currentTime 和 elapsedTime 作为构造函数参数传递意味着您应该将它们设置为全局变量,例如:

public TimeCalculations(long currentTime, long elaspedTime) {
    this.elaspedTime = elapseTime;
    this.currentTime = currentTime;
}

在您的第一个构造函数中,您只需设置 currentTime 两次。更改为:

public TimeCalculations() {
    // since there is no local var named currentTime, you don't need
    // to put this.
    currentTime = System.currentTimeMillis();
}

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2018-09-28
相关资源
最近更新 更多