import java.util.ArrayList;
import java.util.Scanner;
public class employeePayReport
{
public static Scanner sc = new Scanner(System.in);
private static ArrayList<ArrayList<String> > console = new ArrayList<ArrayList<String>>();
static int currState = 1;
public employeePayReport() {
this.console = console;
String[] record ={employee.name(),employee.hours(),employee.sales(),employee.rate(), employee.weeklySalary()}; //error: Undeclared Variable: employee (error for all the elements of the array)
record.add(employee.name());
//how to connect this object to the object created in employeeType method
console.add(record); //error: incompatible types: java.lang.String[] cannot be converted to java.util.ArrayList<java.lang.String>
}
public static void print(){
System.out.println();
System.out.println("\t Name Class Hours Sales Rate Weekly Pay");
System.out.println();
// I have to add coordinates of the array list
System.out.println("\t ======================================================================================");
// I have to add coordinates of the array list
System.out.println("\t ======================================================================================");
// I have to add coordinates of the array list
System.out.println();
}
public static void employeeType(){
System.out.println("Enter field type- ");
System.out.println("1 - Salaried Employee");
System.out.println("2 - Hourly Employees");
System.out.println("3 - Commission Based Employees");
int type = sc.nextInt();
if(type == 1){
salariedEmployees employee = new salariedEmployees();
}else if (type == 2){
hourlyEmployees employee = new hourlyEmployees();
}else if (type == 3){
commissionedEmployees employee = new commissionedEmployees();
}else{
System.out.println("Select valid type");
}
}
public static void main(String[] args){
//I have to edit this method...
while(currState == 1){
print();
System.out.println("Enter a name: ");
String name = sc.next();
employeeType();
}
}
}
如果您查看我的employeePayReport建構式,我會收到所有員工變數的錯誤。錯誤說我沒有宣告變數employee。我想將此變數連接到我在employeeType方法中創建的物件。如何授予對employee建構式創建的物件的訪問權限employeePayReport?
uj5u.com熱心網友回復:
我建議您創建一個abstract classEmployee ,它與您的其他三個類salariedEmployees、hourlyEmployees和commissionedEmployees具有共同屬性,并且還具有 getter 和 setter,例如:
public abstract class Employee {
private String name;
private String hours;
private String sales;
private String rate;
private String weeklySalary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
.
.//getters/setters
.
}
然后創建擴展此類的其他三個類,例如:
public class commissionedEmployees extends Employee{
//specific attribute
}
正如@OldProgrammer 在評論中所說,在 EmployeePayReport 類中,您可以將 Employee 類宣告為 Constructor 引數:
public EmployeePayReport(Employee employee) {
this.console = console; // does not make sense you assign the console to it's self
String[] record ={employee.getName(),employee.getHours(),employee.getName(),employee.getRate(), employee.getWeeklySalary()}; //error: Undeclared Variable: employee (error for all the elements of the array)
//record.add(employee.getName() ""); // you can't do that because does not have function add
//how to connect this object to the object created in employeeType method
//to avoid this error you can use Arrays.asList(record) to convert array to list
console.add(new ArrayList<String>(Arrays.asList(record))); //error: incompatible types: java.lang.String[] cannot be converted to java.util.ArrayList<java.lang.String>
}
注意:我解釋了您在評論中遇到的錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/447653.html
