Java: why can’t my variables be passed to the constructors available to my methods?

So I declared some variables in main and created a new object; Pass a variable as a parameter to the constructor Now, when I call a method in a class, I think that these variables can be accessed by methods instead of passing them to them as parameters. Isn't that right?

This is the code:

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);

    }

}

This is winechiller Java class:

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));

    }

}

Why can't getchillingtime resolve winetemp and so on to variables?

Editor added: Thank you very much for your advice But there's another warning! The description seems to imply that the winechiller class should only contain constructor and method getchillingtime, and getchillingtime should not take parameters! Are there spelling errors in the job file?

Solution

Although this may be implemented in languages such as Scala or Ceylon (see below), in Java, you must explicitly assign constructor parameters to instance variables Thus:

public class WineChiller {

    double wineTemp;
    double preferredTemp;
    double chillTemp;

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

        getChillingTime();
    }

Constructor parameters are visible only within the scope of the constructor The fact that the constructor calls getchillingtime () is irrelevant If you want them to be visible within the scope of the winechiller instance, you must create members in the class All methods of the class can then access instance members

Anyway, I strongly recommend that you read the Java Tutorial carefully This is a:

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

Constructors for other JVM languages

I think you are mainly trying to solve the lengthy problem of Java. You must explicitly copy the constructor parameters to the instance field to implement encapsulation Other languages solve this problem more gracefully, where constructors can be implicitly defined with the class itself However, they will still be converted to the equivalent of the above java code For example:

Scala:

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

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

Examples from: http://joelabrahamsson.com/learning-scala-part-four-classes-and-constructors/

Ceylon

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

Examples from: http://ceylon-lang.org/documentation/1.0/spec/html_single/

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>