有人可以解释一下变量在Java开始时做什么吗?我对我为我的计算机类所做的代码中的变量指出的内容感到困惑。我太新了,甚至不知道要搜索什么来得到正确的答案,而我正在使用的书并没有帮助解释任何事情,因为它将所有变量命名为相同的,这使我对从哪里调用的内容感到困惑。 谢谢 乍得
public void setEmpID(int NewID)
将this.empID设置为NewID?
// access modifier is set to public and a class is declared and named Employee
public class Employee
{
private int empID;
private String firstName;
private String lastName;
private double monthlySalary; (Are these to store the variable values from the main method until class creation or are they defining variables in the object that will be created?)
//Constructor intializes class Employee to create object Employee with instance variables above. Must be same name as class
**public Employee(int empID, String firstName, String lastName, double newSalary) //(is this the storage until the class is created or the defining of the variables for the object that will be created?)**
{
//ensures empID is a positive number before assignment
if (empID > 0.0)
//assigns empID to variable "empID"
**this.empID = empID; //where does this assign empID? in the new object or in this class temporarily until the object is created?**
//assigns firstName to variable "firstName"
this.firstName = firstName;
// assigns lastName to variable "lastName"
this.lastName = lastName;
//ensure monthlySalary is a positive number before assignmentand if
//ends constructor
}
**//method that sets the empID(where are these set?)**
public void setEmpID(int newID)
{
this.empID = newID;
}
//method that sets the firstName
public void setFirstName(String newFirst)
{
this.firstName = newFirst;
}
//method that sets the lastName
public void setLastName(String newLast)
{
this.lastName = newLast;
}
//method that sets the monthlySalary for the new obj
public void setMonthlySalary(double newSalary)
{
this.monthlySalary = newSalary;
}
**//Gets empid from the object and returns empID to the calling method (I think this is right)**
public int getEmpID()
{
return empID;
}
//gets first name from the object and returns first name to the calling method
public String getFirstName()
{
return firstName;
}
//gets last name from the object and returns last name to the calling method
public String getLastName()
{
return lastName;
}
//gets monthly salary from the object and returns it to the calling method
public double getMonthlySalary()
{
return monthlySalary;
}
}
答案 0 :(得分:1)
如果您想了解变量可见性,这可能有所帮助。 SO Question
至于变量具有相同名称时,请参阅下面的代码
<强> Example.java 强>
public class Example {
private int empID = 0; //Creates a global variable
public Example(int empID) { //Creates a local variable that is
//initiated when contructor is called
this.empID = empID; //Calls the global variable and sets it
//equal to the local variable
exampleMethod(); //Calls the method below
}
private void exampleMethod() {
empID = 1; //Sets the global variable to = 1
//This is the same as this.empID = 1;
}
public void setEmpID(int newID) {
empID = newID; //Sets the global variable to = newID
//This is the same as this.empID = newID;
}
public int getEmpID() {
return empID; //Gets the value of the global variable
//empID. Example use below
}
}
<强> Example2.java 强>
public class Example2 {
public Example2() {
Example ex = new Example(1); //Create and initate an Example class
System.out.println(ex.getEmpID()); //Prints out Example.empID, aka 1
ex.setEmpID(2);
System.out.println(ex.getEmpID()); //Prints out Example.empID, aka 2
}
public static void main(String[] args) {
new Example2(); //Creates and initates an Example2 class
}
}
至于变量类型,这个简短的教程将介绍它们。 Tutorial