从ProductionWorker1类创建对象并使用setter和getter

时间:2017-10-18 21:04:13

标签: java inheritance getter-setter

我需要能够获得键盘输入并将其传递给从ProductionWorker1类创建的对象。 ProductionWorker1类扩展Employee1类以获取名称,编号和日期(已雇用)。 ProductionWorker1类有一个带有shift和pay的默认构造函数。 Employee1构造函数具有名称,编号和日期。如果有人可以提供帮助,我真的很感激。

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());
  }
}

1 个答案:

答案 0 :(得分:0)

这将帮助您入门。对不起,但我被跟踪了。在main中,找出如何将字符串转换为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);

}