Java中的可变范围和可见性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中的可变范围和可见性相关的知识,希望对你有一定的参考价值。

我正在度假和度假。假期文件将作为我的蓝图,驱动程序将是创建假期实例的程序的交互部分。我把所有东西都完美地运行了,但是当我在度假驱动程序类的第47行的print语句中添加一个值时,我就打破了程序。

我需要调用我认为在第42行声明的numSales的值。当我在输出的开头和中间的第47行输入numSales时,如下所示我得到一条红线,Eclipse告诉我“numSales不能被解析为变量“。我需要做什么才能在度假驱动程序的第47行的print语句中主动输出numSales的值?

这是度假班:

    package cbrownmod4;

    import java.text.NumberFormat;

    public class Vacation {

// money formatting
NumberFormat money = NumberFormat.getCurrencyInstance();

// instance variables
private String vacationName;
private int numSold;
private Double priceEach;

// empty constructor
public Vacation() {
}

// partial constructor
public Vacation(String n, int s, double e) {
    vacationName = n;
    numSold = s;
    priceEach = e = 0;
}

// updatSales method
public int updateSales() {
    int updateSales = 0;
    updateSales = updateSales + numSold;
    return updateSales;
}

// totalValue method
public double totalValue() {
    double totalValue = 0;
    totalValue = totalValue + (numSold * priceEach);
    return totalValue;
}

// toString method
public String toString() {
    return vacationName + " has been sold " + numSold + " times for " + money.format(priceEach) + 
            " each for a total value of " + money.format(numSold*priceEach);
}

// getters and setters
public String getVacationName() {
    return vacationName;
}

public void setVacationName(String vacationName) {
    this.vacationName = vacationName;
}

public int getNumSold() {
    return numSold;
}

public void setNumSold(int numSold) {
    this.numSold = numSold;
}

public Double getPriceEach() {
    return priceEach;
}

public void setPriceEach(Double priceEach) {
    this.priceEach = priceEach;
}

}

这是假期司机:

    package cbrownmod4;

   import java.text.NumberFormat;
   import java.util.Scanner;

   public class VacationDriver {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    NumberFormat money = NumberFormat.getCurrencyInstance();

    // ask for the number of vacations and read it into numVacations
    System.out.println("How many vacations are there?:");
        int numVacations = input.nextInt();

    // ask for the number of sales people and read it into numPeople
    System.out.println("How many sales people are there?: ");
        int numPeople = input.nextInt();

    // beginning of loop for number of vacations
    for(int i = 0; i < numVacations; i++) { 

        //ask for the name and price and read these into variables
        System.out.println("What is the name of vacation #" + Integer.toString(i+1) + "?:");
            String name = input.next();

        System.out.println("How much does " + name + " cost?: ");
            Double price = input.nextDouble();

        // create a Vacation instance using the data obtained above
        Vacation vacay = new Vacation (name, i, price); 

        // loop through the sales people    
        for(int j = 0; j < numPeople; j++) {

            // ask for the sales for the current vacation and read it in
            System.out.println("What are the sales for the current vacation by person #" + Integer.toString(j+1) + "?:");
                int numSales = input.nextInt(); // line 42

            // call updateSales with this number
            numSales = vacay.updateSales(); 

        } //end of inner loop
        //where line 47 begins  
        //print out this vacation info
        System.out.println(numSales + " trips to " + name + " were sold for " + money.format(price) + " for a total of " + money.format(numSales * price));

    } //end of outer for loop

} //end of main

}

由于要求提供代码部分代码片段不起作用,这有点证明了我的问题:

    //print out this vacation info
        System.out.println(numSales + " trips to " + name + " were 
    sold for " + money.format(price) + " for a total of " + 
    money.format(numSales * price));

注意:如果您取出休假驱动程序片段中的numSales,程序正确执行但没有正确的输出,因为它缺少必要的输出变量

简明扼要的问题是 - 为什么当我使用它时,numSales不起作用,就像在短片段中所示。我的问题再一次是Eclipse说“numSales无法解析为变量”。

答案

问题是你在for {}块中声明了numSales

您需要在for块之前声明它:

    int numSales = 0;  // set initial value in case numPeople is 0 and the loop never runs

    // loop through the sales people    
    for(int j = 0; j < numPeople; j++) {

        // ask for the sales for the current vacation and read it in
        System.out.println("What are the sales for the current vacation by person #" + Integer.toString(j+1) + "?:");
        numSales = input.nextInt();  // line 42; this value is never used?  it is overwritten below

        // overwrite the never-used value from line 42?? 
        numSales = vacay.updateSales(); 

    } //end of inner loop

    // now numSales is still visible, because it was declared on this same 'level' and not in an inner block
    System.out.println("numSales: " + numSales);

第42行中设置的值从未使用过,并且在第45行中被覆盖,因此您也可以调用input.nextInt();而不将值设置为numSales

以上是关于Java中的可变范围和可见性的主要内容,如果未能解决你的问题,请参考以下文章

Java中同步的可见性效果

对Java中的安全发布和可见性感到困惑,尤其是使用Immutable Objects

如何从活动中更改片段中视图的可见性

Android:通过更改片段更改菜单项的可见性

块处理程序中变量的可见性(范围)

显示/隐藏片段并以编程方式更改可见性属性