繁体   English   中英

未使用的局部变量的值

[英]value of the local variable not used

我正在学习Java,并且在课堂上我们开始使用多个类。...我的雇员类具有默认构造函数。 在其中,我具有默认值的变量。 我的问题是未使用默认构造函数中变量的值,因此在变量下出现黄色下划线。 有人可以指引我正确的方向。

package week2;

import java.text.NumberFormat;

public class Employee {

    //Instance Variables
    private String firstName = "", lastName = "";
    private char gender = 'U';
    private int dependents = 0;
    private double annualSalary = 0;

    //Default Employee Constructor 
    public Employee() {
        String firstName = "not given";
        String lastName = "not given";
        char gender = 'U';
        int dependents = 0;
        double annualSalary = 20000;
    } //End of default Employee Constructor

    //Employee Constructor with variables
    public Employee(String firstName, String lastName, char gender, int dependents, double salary) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.dependents = dependents;
        this.annualSalary = salary;
    } //End of Overloaded Employee Constructor

    //Calculate Pay
    public double annualSalary() {
        return (getAnnualSalary() / 52);
    } //End Calculate Pay

    //Display Employee
    public void displayEmployee() {
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        System.out.println("First Name: \t" + this.firstName);
        System.out.println("Last Name: \t" + this.lastName);
        System.out.println("Gender: \t" + this.gender);
        System.out.println("Dependents: \t" + this.dependents);
        System.out.println("Annual Salary: \t" + nf.format(this.annualSalary));

        System.out.println("Weekly Pay: \t" + nf.format(annualSalary()));
    } //End Display Employee


    //Getters and Setters
    //Get-Set firstName
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    //Get-Set lastName
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    //Get-Set Gender
    public char getGender() {
        return gender;
    }
    public void setGender(char gender) {
        this.gender = gender;
    }

    //Get-Set dependents
    public int getDependents() {
        return dependents;
    }
    public void setDependents(int dependents) {
        this.dependents = dependents;
    }

    //Get-Set annualSalary
    public double getAnnualSalary() {
        return annualSalary;
    }
    public void setAnnualSalary(double annualSalary) {
        this.annualSalary = annualSalary;
    }
}

您正在默认构造函数中创建local variables 删除声明,它将为类级变量赋值。

//Default Employee Constructor 
public Employee() {
    firstName = "not given";
    lastName = "not given";
    gender = 'U';
    dependents = 0;
    annualSalary = 20000;
} //End of default Employee Constructor

注意:黄色表示警告。 在您的情况下,它暗示您正在创建局部变量并为其分配值,这在您的构造函数中未使用,并且局部变量在构造函数之外不可见。


如果未使用相同的名称定义其他本地变量,则可以使用this.firstNamefirstName (在您的情况下,您可以在默认构造函数中同时使用它们)

如果使用相同的名称定义了某些局部变量,则firstName指代局部变量,而this.firstName指代类级变量。(您可以在参数化的构造函数中看到这一点)

正如编译器所说,no-arg构造函数声明新变量,它们被初始化但未被使用。 请注意,这些变量不是类中的字段。

public Employee() {
    //this is a local variable in the method
    String firstName = "not given";
    //this variable is not the same as this:
    this.firstName = "not given";
    //same for other variables
}

这种情况有两种解决方案:

  1. 删除所有局部变量的声明,并改用字段:

     public Employee() { this.firstName = "not given"; //... } 
  2. 使用this()并使用字段的初始值调用其他构造函数:

     public Employee() { this("not given", "not given", 'U', 0, 2000); } 

在这里,您必须清楚的概念是变量的范围,在默认构造函数中

 //Default Employee Constructor 
public Employee() {
    String firstName = "not given";
    String lastName = "not given";
    char gender = 'U';
    int dependents = 0;
    double annualSalary = 20000;
} //End of default Employee Constructor

声明的变量仅在此构造函数中具有作用域,它们不同于在类的开头声明的实例变量。 因此,这些变量不会在任何地方使用,因此会出现编译器警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM