2017-10-18 45 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); 

}