Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建employee类和employee类演示?_Java_Class_Methods - Fatal编程技术网

Java 创建employee类和employee类演示?

Java 创建employee类和employee类演示?,java,class,methods,Java,Class,Methods,我们必须使用set和get方法来 Employee() 员工(empId:int) Employee(empId:int,payRate:double,weeklyHrs:double) setId(empId:int):void setPayRate(payRate:double):无效 setHrs(周报:双倍):无效 getId():int getPayRate():双倍 getHrs():double getWeeklyPay():double toString():字符串 创建em

我们必须使用set和get方法来

  • Employee()
  • 员工(empId:int)
  • Employee(empId:int,payRate:double,weeklyHrs:double)
  • setId(empId:int):void
  • setPayRate(payRate:double):无效
  • setHrs(周报:双倍):无效
  • getId():int
  • getPayRate():双倍
  • getHrs():double
  • getWeeklyPay():double
  • toString():字符串
创建employee类和employee类演示。我们必须使用两个员工id,输入他们的每周工作时间和工资,以返回他们的收入。以下是我为Employee类准备的内容:

      //@param
    Scanner input = new Scanner(System.in); //Scanner object
    public void setID(int empID){
      System.out.println("Enter employee 1's id: ");
      String input2 = input.next();
      int emp1 = Integer.parseInt(input2);
      System.out.println("Enter employee 2's id: ");
      String input3 = input.next();
      int emp2 = Integer.parseInt(input3);     
    }
    //@param
    public void setPayRate(double payRate){
      System.out.println("Enter employee 1's payrate: ");
      String input4 = input.next();
      double pay1 = Double.parseDouble(input4);
      System.out.println("Enter employee 2's payrate: ");
      String input5 = input.next();
      double pay2 = Double.parseDouble(input5);  
    } 
    //@param
    public void setHrs(double weeklyHrs){
      System.out.println("Enter employee 1's hours: ");
      String input6 = input.next();
      double hour1 = Double.parseDouble(input6);
      System.out.println("Enter employee 2's hours: ");
      String input7 = input.next();
      double hour2 = Double.parseDouble(input7); 
    }

//GET METHODS
    public int getID(int empID){
      return empID;  
    }
    public double getHrs(double hours){
      return hours;
    }
    public double getWeeklyPay(double pay){
      return pay;
    }

//DISPLAY ANSWERS


    public String toString(int emp1,int emp2,double hour1,double hour2,double pay1,double pay2){

      String myString= String.format("The first employee's ID is: " + emp1 + 
                                     "The second employee's ID is: " + emp2 +
                                     "The first employee's hours are: " + hour1 +
                                     "The secondemployee's hours are: " + hour2 +
                                     "The first employee's pay: $%,.2f"+ pay1*hour1 +
                                     "The second employee's pay: $%,.2f" + pay2*hour2,
                                     emp1,emp2,hour1,hour2,pay1,pay2);
      return myString;

    }

  }//End of class
我只是想知道我会在Employee类演示中添加什么,以便它能够返回toString方法?

System.out.println()
在传入的任何对象上自动调用
toString()
方法-或者,您可以自己调用它:

Employee e;

// properly initialize e

System.out.println(e); // works
System.out.println(e.toString()); // also works
作为补充说明,您的
toString()
方法不正确。它应该是
Employee
类的一个成员函数——将其作为一个接受
Employee
对象的方法是错误的(假设您希望遵循标准的Java OOP实践)

正确的方法如下所示:

// Notice no arguments are passed in
public String toString() {

    // Instead, we use the "this" variable
    return String.format("My ID is %s, my hours are %f, and my pay is $%,.2f", this.empID, this.hours, this.pay);
}
System.out.println()
对传入的任何对象自动调用
toString()
方法-或者,您可以自己调用它:

Employee e;

// properly initialize e

System.out.println(e); // works
System.out.println(e.toString()); // also works
作为补充说明,您的
toString()
方法不正确。它应该是
Employee
类的一个成员函数——将其作为一个接受
Employee
对象的方法是错误的(假设您希望遵循标准的Java OOP实践)

正确的方法如下所示:

// Notice no arguments are passed in
public String toString() {

    // Instead, we use the "this" variable
    return String.format("My ID is %s, my hours are %f, and my pay is $%,.2f", this.empID, this.hours, this.pay);
}

我看不到您的代码中定义了任何类。我看不到您的代码中定义了任何类。