【问题标题】:Creating object from ProductionWorker1 class and using setters and getters从 ProductionWorker1 类创建对象并使用 setter 和 getter
【发布时间】:2017-10-18 21:04:12
【问题描述】:

我需要能够获取键盘输入并将其传递给从 ProductionWorker1 类创建的对象。 ProductionWorker1 类扩展了 Employee1 类以获取姓名、编号和日期(雇用)。 ProductionWorker1 类有一个带班次和工资的默认构造函数。 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());
  }
}

【问题讨论】:

  • ???查看 main 的最后一行。您正在将一个类设置为一个新实例,并在设置对象之前发送该对象名称的构造函数。您不是要传递字符串名称吗?
  • @older coder 我更改了变量 shift 并支付给 int 和 double 然后将 int 和 double 传递给 obj: int shift;双薪; ProductionWorker1 obj = new ProductionWorker1(int, shift);这行得通。你知道如何使用其他类的 setter 和 getter 吗?因为我只是在 Main 中使用变量?
  • 您说Employee1 构造函数接受名称、数字和日期,但它只接受名称。此外,您的类没有任何属性。

标签: java inheritance getter-setter


【解决方案1】:

这将使您入门。对不起,但我走偏了。在 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);

}

【讨论】:

    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 2019-11-26
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多