Java:为什么我的变量不能传递给我的方法可用的构造函数? [英] Java: Why aren't my variables passed to the constructor usable by my method?

查看:126
本文介绍了Java:为什么我的变量不能传递给我的方法可用的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在main中声明了一些变量并创建了一个新对象;将变量作为参数传递给构造函数。现在,当我在类中调用一个方法时,我会认为这些变量可以被方法访问,而不必将它们作为参数传递给它。这不是这样吗?

So I declared some variables in main and created a new object; passing the variables as arguments to the constructor. Now, when I call a method inside the class, I would have thought that these variables would be accessible by the method, WITHOUT having to pass them as arguments to it. Is this not so?

这是代码:

import java.util.Scanner;

public class Step2_lab11 {

    public static void main(String[] args) {

        final int TAO = 50;
        final double DELTA_MINUTES = 0.1;

        System.out.println("VINETS AVKYLNINGSTID \n");
        System.out.println("Ange vinets temperatur:");

        Scanner userIn = new Scanner(System.in);
        double wineTemp = userIn.nextDouble();

        System.out.println("Vinets önskade temperatur:");
        double preferredTemp = userIn.nextDouble();

        System.out.println("Kylens/frysens temperatur:");
        double chillTemp = userIn.nextDouble();

        WineChiller wineChiller = new WineChiller(wineTemp, preferredTemp, chillTemp);

    }

}

这是班级WineChiller.java:

Here's the class WineChiller.java:

public class WineChiller {

    public WineChiller(double wineTemp, double preferredTemp, double chillTemp) {
        getChillingTime();

    }

    public void getChillingTime() {

        while(wineTemp>preferredTemp)
        {
            elapsedTime += DELTA_MINUTES;
            double dT = (wineTemp - chillTemp) * DELTA_MINUTES  / TAO;
            wineTemp -= dT;
        }
        System.out.println(Math.round(elapsedTime));

    }

}

为什么可以' t getChillingTime将wineTemp等解析为变量吗?

Why can't getChillingTime resolve wineTemp etc to variables?

编辑添加:非常感谢指针人。但还有一个警告!说明似乎暗示WineChiller类应该只包含构造函数和方法getChillingTime,并且getChillingTime不应该接受参数!在作业文件中是否有拼写错误?

EDITED TO ADD: Much appreciate the pointers guys. But there's an additional caveat! The instructions seems to imply that the class WineChiller should ONLY contain the constructor and the method getChillingTime, and that getChillingTime SHOULD NOT take arguments! Is there a typo in the assignment paper?

推荐答案

虽然这可能在Scala或Ceylon 等语言中成为可能(请参阅下文),在Java中,您必须明确地将构造函数参数分配给实例变量。因此:

While this would probably be possible in languages like Scala or Ceylon (see below), in Java, you have to assign constructor arguments explictly to instance variables. Thus:

public class WineChiller {

    double wineTemp;
    double preferredTemp;
    double chillTemp;

    public WineChiller(double wineTemp, double preferredTemp, double chillTemp) {
        this.wineTemp = wineTemp;
        this.preferredTemp = preferredTemp;
        this.chillTemp = chillTemp;

        getChillingTime();
    }

构造函数参数仅在构造函数的范围内可见。构造函数调用 getChillingTime()这一事实无关紧要。如果您希望它们在 WineChiller 实例的范围内可见,则必须在该类中创建成员。然后,该类的所有方法都可以访问实例成员。

Constructor arguments are visible only in the scope of the constructor. The fact that the constructor calls your getChillingTime() is irrelevant. If you want them to be visible within the scope of a WineChiller instance, you'll have to create members in that class. All methods of that class can then access instance members.

无论如何,我强烈建议您仔细阅读Java教程。这是一个:

Anyway, I strongly suggest you thoroughly read through a Java tutorial. Here's one:

http://docs.oracle .com / javase / tutorial

我想你是主要是为了解决Java的冗长问题,你必须将构造函数参数显式复制到实例字段中才能实现封装。其他语言更优雅地解决了这个问题,其中构造函数可以与类本身一起隐式定义。但是,它们仍将转换为与上述Java代码等效的内容。例如:

I think you're mainly struggling with the verbosity of Java, where you have to explicitly copy constructor arguments onto instance fields in order to implement encapsulation. Other languages have solved this more elegantly, where constructors can be implicitly defined along with the class itself. However, they would still translate to something equivalent of the above Java code. For instance:

class Greeter(message: String) {
    def SayHi() = println(message)
}

val greeter = new Greeter("Hello world!")
greeter.SayHi()

此处示例: http://joelabrahamsson.com/learning-scala-part-four-classes-and-constructors/

class Point(Float x, Float y) { ... }
object origin extends Point(0.0, 0.0) {}

此处的示例: http://ceylon-lang.org /documentation/1.0/spec/html_single/

这篇关于Java:为什么我的变量不能传递给我的方法可用的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆