2017-10-18 77 views
0

我需要能夠獲取鍵盤輸入並將其傳遞給從ProductionWorker1類創建的對象。 ProductionWorker1類擴展了Employee1類以獲取名稱,編號和日期(僱用)。 ProductionWorker1類具有一個包含班次和薪酬的默認構造函數。 Employee1構造函數具有名稱,編號和日期。如果任何人都可以幫助,我會非常感激。從ProductionWorker1類創建對象並使用setter和getters

public class Employee1 
{ 
    public Employee1(String name) 
    { 
    System.out.println("name: " + name); 
    } 
    public void setName(String name) 
    { 
     name = name; 
    } 
    public String getName(String name) 
    { 
     return name; 
    } 
} 

public class ProductionWorker1 extends Employee1 
{ 

    public ProductionWorker1(int shift, double pay) 
    { 
    super(""); 
    System.out.println("shift: " + shift); 
    System.out.println("pay: " + pay); 

    } 
    public void setShift(int shift) 
    { 
    shift = shift; 
    } 
    public int getShift(int shift) 
    { 
    return shift; 
    } 

} 

import java.util.Scanner; 

public class Main 
{ 
    public static void main(String[] s) 
    { 
    String name; 
    String shift; 
    String pay; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("enter name"); 
    name = keyboard.nextLine(); 
    System.out.println("enter shift time"); 
    shift = keyboard.nextLine(); 
    System.out.println("enter pay"); 
    pay = keyboard.nextLine(); 

    ProductionWorker1 obj = new ProductionWorker1(obj.getName()); 
    } 
} 
+0

???檢查主要的最後一行。您正在設置一個新實例的類並在設置對象之前發送構造函數Object的名稱。你不是想傳遞它的字符串名稱嗎? –

+0

@older編碼器我改變變量移位並支付給int和double,然後傳遞int和double到obj: int shift; 雙薪; ProductionWorker1 obj = new ProductionWorker1(int,shift); 這工作。你知道如何使用其他類的setters和getters嗎?因爲我只是在Main中使用變量? –

+0

你說'Employee1'構造函數需要一個名字,數字和日期,但它只取這個名字。另外,你的類沒有任何屬性。 – alejandrogiron

回答

0

這將讓你開始。對不起,但我被追蹤了。主要了解如何將字符串轉換爲int。與工資一樣。簡單地將它們掃描爲它們的類型會更好。

/** 
    * You have setters and getters, but you don't have any class variables to set/get 
    * @author me 
    * 
    */ 

    public class Employee1 { 
    // You don't have any class variables. 
    // I added this one. 
     String name; 

     public Employee1(String name) { 
       // I added this line to set the class variable name to parameter name 
       setName(name); 
       System.out.println("In Constructor Employee1(String) name: " + name); 
     } 

     public void setName(String name) { 
     // I changed this to set the class name to the parameter name 
       this.name = name; 
     } 

     /** 
     * IF You are getting the class variable, you don't need to pass a variable to get it. 
     * @return 
     */ 
     public String getName() { 
       return name; 
     } 
    } 


public class ProductionWorker1 extends Employee1 { 

// Since ProductionWorker1 extends Employee, it will 
// have a name variable. Now add shift and pay for ProductionWorker1 
int shift = 0; 
double pay = 0.0; 

/** 
* Constructor with NO NAME!!! 
* @param shift 
* @param pay 
*/ 
public ProductionWorker1(int shift, double pay) { 
    super(""); // initializes Employee1 and sets name to empty string 
    setShift(shift); 
    setPay(pay); 
    System.out.println("In Constructor ProductionWorker1(int,double)shift: " + shift); 
    System.out.println("pay: " + pay); 

} 

/** 
* I added this Constructor to get the name down to Employee1 
* @param name 
* @param shift 
* @param pay 
*/ 
public ProductionWorker1(String name, int shift, double pay) { 
    super(name); // initializes Employee1 and sets name to ProductionWorker's name 
    setShift(shift); 
    setPay(pay); 
    System.out.println("In Constructor ProductionWorker1(String int,double)shift: " + shift); 
    System.out.println("pay: " + pay); 
} 

public double getPay() { 
    return pay; 
} 
public void setPay(double pay) { 
    this.pay = pay; 
} 

public void setShift(int shift) { 
    this.shift = shift; 
} 

public int getShift() { 
    return shift; 
} 

} 



public class Main { 

public static void main(String[] args) { 
    String name; 
    String shift; 
    String pay; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("enter name"); 
    name = keyboard.nextLine(); 
    System.out.println("enter shift time"); 
    shift = keyboard.nextLine(); 
    System.out.println("enter pay"); 
    pay = keyboard.nextLine(); 

    // !!! You need to create an int from a string OR you could scan it in 
    // as an integer. For now, I'm using a temp int. 
    int tempShift = 5; 
    // same with pay 
    double tempPay = 12.56; 

    ProductionWorker1 obj = new ProductionWorker1(name, tempShift, tempPay); 
    // obj dude got a raise 
    obj.setPay(24.75); 

}