Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我很难弄清楚我的变量从何处调用或从何处调用_Java - Fatal编程技术网

Java 我很难弄清楚我的变量从何处调用或从何处调用

Java 我很难弄清楚我的变量从何处调用或从何处调用,java,Java,有人能解释一下变量在Java中的作用吗?我对我正在为我的计算机课做的代码中的变量指向什么感到困惑。我太新了,甚至不知道要搜索什么才能得到正确的答案,我正在使用的这本书也没有帮助解释任何事情,因为它对所有变量的名称都是相同的,这让我对从何处调用感到困惑。 非常感谢。 查德 public void setempidit NewID在哪里将this.empID设置为NewID // access modifier is set to public and a class is declared and

有人能解释一下变量在Java中的作用吗?我对我正在为我的计算机课做的代码中的变量指向什么感到困惑。我太新了,甚至不知道要搜索什么才能得到正确的答案,我正在使用的这本书也没有帮助解释任何事情,因为它对所有变量的名称都是相同的,这让我对从何处调用感到困惑。 非常感谢。 查德

public void setempidit 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;          
    }

}

如果您想了解可变可见性,这可能会有所帮助

当变量具有相同名称时,请参见下面的代码

Example.java

示例2.java


至于变量类型,本简短教程将介绍它们

那么全局变量在这个程序的主方法中是什么?我定义为employee employeeTwo=new employee100,John,Doe,3000.01我知道第一个employee告诉调用employee类,第二个告诉main方法从名为employeeOne的类创建一个对象,它将是一个新的employee,具有括号中的属性是全局变量吗?谢谢你们的快速反应!!你们太棒了!我不太清楚你的第一个问题是什么意思。全局变量可以被整个类看到,而局部变量只能被单个方法或构造函数看到。@chadjensen,关于第二部分。Employee employeeTwo创建一个变量。雇员二=新雇员100,约翰,多伊,3000.01;启动变量以便可以使用它。例如,在我的代码示例中,如果您运行example ex;然后是System.out.printlnex.getEmpID;你会得到一个error@chadjensen这有助于澄清一切吗?丹,你的例子太棒了。谢谢你花时间给我解释。它帮了很多忙。只是想让你知道你的评论不需要**。谢谢。我想把我有疑问的部分加粗,以便更容易地看到这些部分,这样阅读文章的人就可以很快找到有问题的代码。出于某种原因,当我**这些部分在发布时不是黑体。哦,那是因为它们是缩进的,所以问题不能识别为黑体。我的回答有助于澄清问题吗?我刚刚找到了原因。括号被论坛软件看到了,它只是逐字地发布了介于两者之间的所有内容。
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
    }
}
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
    }
}