-1

我现在只是在用 Java 做 OOP,而且我遇到了一些问题。我有 2 个问题,最大的是对象没有正确显示。在我的employee.java 类中,我有以下代码用于显示员工/工人。

public String getInfo() {
        return firstName + " " + lastName + "\n" + address + "\n" + city + 
                                            "\n" + wageRate + "\n" + hoursWorked;
    }

现在,在employeeTest.java我进行大部分处理和更新的地方,我为单独的对象创建了以下内容:

//instantiate 4 employee objects
        employees worker1 = new employees();
        employees worker2 = new employees();
        employees worker3 = new employees();
        employees worker4 = new employees();

之后,我尝试更新员工并在使用以下代码后立即显示它们(由于测试原因,它不完整)。

    //declare variables to update employees
    String newFirstName1, newFirstName2, newFirstName3, newFirstName4;
    String newLastName1, newLastName2, newLastName3, newLastName4;
    String newAddress1, newAddress2, newAddress3, newAddress4;
    String newCity1, newCity2, newCity3, newCity4;
    int newHoursWorked1, newHoursWorked2, newHoursWorked3, newHoursWorked4;
    int newWageRate1, newWageRate2, newWageRate3, newWageRate4;

    //update the employee (in progress)
    System.out.println("Please enter Employee 1's first name:");
    newFirstName1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's last name:");
    newLastName1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's address:");
    newAddress1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's city:");
    newCity1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's hours worked:");
    newHoursWorked1 = conIn.nextInt();
    System.out.println("Please enter Employee 1's wage rate:");
    newWageRate1 = conIn.nextInt();
    worker1.setFirstName(newFirstName1);
    worker1.setLastName(newLastName1);

    //Display the employees
    System.out.println("Employee 1");
    System.out.println("----------------------");
    System.out.println(worker1.getInfo());
    System.out.println("Employee 2");
    System.out.println("----------------------");
    System.out.println(worker2.getInfo());
    System.out.println("Employee 3");
    System.out.println("----------------------");
    System.out.println(worker3.getInfo());
    System.out.println("Employee 4");
    System.out.println("----------------------");
    System.out.println(worker4.getInfo());

但是,这会更新所有工作人员。前任。我输入“John”作为名字,worker1-4都更新为名字“John”。我只想worker1更新,所以我知道它有效。

第二个问题是在执行和测试代码时跳过了以下代码。

    //change the company name
    String newCompanyName;
    System.out.println("Please enter a new company name:");
    newCompanyName = conIn.nextLine();
    employees.setCompanyName(newCompanyName);

编辑 3

employeeTest.java下面的代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeerionmurphy;
import employeerionmurphy.employees.*;
import java.util.*;

/**
 *
 * @author Owner
 */
public class employeeTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //declare scanner
        Scanner conIn = new Scanner(System.in);

        //instantiate 4 employee objects
        employees worker1 = new employees();
        employees worker2 = new employees();
        employees worker3 = new employees();
        employees worker4 = new employees();

        //Display the employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //declare variables to update employees
        String newFirstName1, newFirstName2, newFirstName3, newFirstName4;
        String newLastName1, newLastName2, newLastName3, newLastName4;
        String newAddress1, newAddress2, newAddress3, newAddress4;
        String newCity1, newCity2, newCity3, newCity4;
        int newHoursWorked1, newHoursWorked2, newHoursWorked3, newHoursWorked4;
        int newWageRate1, newWageRate2, newWageRate3, newWageRate4;

        //update the employee (in progress)
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //upgrade the wage rate of the employee that has a wage rate of zero
        //(in progress)


        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the employees name and gross pay
        System.out.println(employees.getTotalPay());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //change the company name
        String newCompanyName;
        System.out.println("Please enter a new company name:");
        newCompanyName = conIn.nextLine();
        employees.setCompanyName(newCompanyName);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Built by Rion Murphy");

    }
}

employee.java下面的代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeerionmurphy;

import java.util.*;
/**
 *
 * @author Owner
 */
public class employees {
    //declare variables
    static String firstName;
    static String lastName;
    static String address;
    static String city;
    static int hoursWorked;
    static int wageRate;
    final double OVERTIME = 40.00;

    public static String companyName = "Grand Company";

    //declare a constructor to initalize all fields to null
    public employees() {
    }

    //declare a constructor to hold the actual values in the variables.
    public employees(String newFirstName, String newLastName, 
            String newAddress, String newCity, int newHoursWorked, int newWageRate) {
        firstName = newFirstName;
        lastName = newLastName;
        address = newAddress;
        city = newCity;
        hoursWorked = newHoursWorked;
        wageRate = newWageRate;

    }

    //accessors for the variables
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public String getAddress() {
        return address;
    }
    public String getCity() {
        return city;
    }
    public int getHoursWorked() {
        return hoursWorked;
    }
    public int getWageRate() {
        return wageRate;
    }

    //mutators for hoursWorked and wageRate
    public void setHoursWorked(int newHoursWorked) {
        hoursWorked = newHoursWorked;
    }
    public void setWageRate(int newWageRate) {
        wageRate = newWageRate;
    }

    //mutators for the string variables
    public void setFirstName(String newFirstName) {
        firstName = newFirstName;
    }
    public void setLastName(String newLastName) {
        lastName = newLastName;
    }
    public void setAddress(String newAddress) {
        address = newAddress;
    }
    public void setCity(String newCity) {
        city = newCity;
    }

    //company name mutator
    public String getCompanyName() {
        return companyName;
    }
    public static void setCompanyName(String newCompanyName) {
        companyName = newCompanyName;
    }

    //printing out employee information
    public String getInfo() {
        return firstName + " " + lastName + "\n" + address + "\n" + city + "\n" + wageRate + "\n" + hoursWorked;
    }

    //printing out employee name and totalPay
    public static String getTotalPay() {
        //calculate total pay
        double totalPay;
        int overtimeHours;
        if (hoursWorked > 40) {
            overtimeHours = hoursWorked - 40;
            totalPay = (overtimeHours * (wageRate * 1.5)) + (40 * wageRate);
        } else {
            totalPay = hoursWorked * wageRate;
        }
        //return the name and the total pay of employee
        return firstName + " " + lastName + "\n" + totalPay;
    }

    //console operations done below
    public static void main(String[] args) {
        // TODO code application logic here

        //Prompt user for all data (use previous mutators to set hoursWorked and wageRate
        Scanner conIn = new Scanner(System.in);
        System.out.println("Please enter your first name: ");
        firstName = conIn.nextLine();
        System.out.println("Please enter your last name: ");
        lastName = conIn.next();


//        Errors detected in the below line of code, will repair at a later date
//        
        System.out.println("Please enter your address: ");
        address = conIn.nextLine();       
        System.out.println("Please enter your city: ");
        city = conIn.nextLine();


        System.out.println("Please enter your hours worked: ");
        hoursWorked = conIn.nextInt();
        System.out.println("Please enter your wage rate: ");
        wageRate = conIn.nextInt();
        System.out.println("");

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //print the fields to the console (except the constant)
        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
        System.out.println("Address: " + address);
        System.out.println("City: " + city);
        System.out.println("Hours Worked: " + hoursWorked);
        System.out.println("Wage Rate: " + wageRate);
        System.out.println("Company Name: " + companyName);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //calculate and return gross pay (time and a half over 40 hours)
        double totalPay;
        int overtimeHours;
        if (hoursWorked > 40) {
            overtimeHours = hoursWorked - 40;
            totalPay = (overtimeHours * (wageRate * 1.5)) + (40 * wageRate);
        } else {
            totalPay = hoursWorked * wageRate;
        }
        System.out.println("The total pay for " + hoursWorked + " hours is " + totalPay);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");
    }
}
4

1 回答 1

2

是的,它们是静态的

static String firstName;
static String lastName;
static String address;
static String city;
static int hoursWorked;
static int wageRate;

摆脱static修饰符。 static只有一个变量,而不是每个实例一个。

当您更改静态变量时,就像firstName更改firstName类本身一样。所有实例都将使用相同的firstName.

于 2013-09-11T16:30:57.227 回答