陷入这个Java程序。请解释为什么在EmpBusinessLogic.java类中调用EmployeeDetails

时间:2016-02-07 09:30:30

标签: java encapsulation

我正在学习Java Encapsulation。我在看到这两个程序的时候遇到了困难。 请向我解释在EmployeeDetails课程中调用EmpBusinessLogic.java的方式和原因。 也就是说,

public double calculateYearlySalary(EmployeeDetails employeeDetails) 

在上面的行中,EmployDetails是如何调用的? 我很抱歉问这么愚蠢的问题,但我被困在这里。 非常感谢您的支持!

EmployeeDetails.java

    public class EmployeeDetails {

           private String name;
           private double monthlySalary;
           private int age;

           // @return the name

           public String getName() {
              return name;
           }

           // @param name the name to set

           public void setName(String name) {
              this.name = name;
           }

           // @return the monthlySalary

           public double getMonthlySalary() {
              return monthlySalary;
           }

           // @param monthlySalary the monthlySalary to set

           public void setMonthlySalary(double monthlySalary) {
              this.monthlySalary = monthlySalary;
           }

           // @return the age

           public int getAge() {
              return age;
           }

           // @param age the age to set

           public void setAge(int age) {
           this.age = age;
           }
        }

EmpBusinessLogic.java:

 public class EmpBusinessLogic {

           // Calculate the yearly salary of employee
           public double calculateYearlySalary(EmployeeDetails employeeDetails) //How and Why EmployeeDetails is taken here?
{
              double yearlySalary = 0;
              yearlySalary = employeeDetails.getMonthlySalary() * 12;
              return yearlySalary;
           }

           // Calculate the appraisal amount of employee
           public double calculateAppraisal(EmployeeDetails employeeDetails){

              double appraisal = 0;

              if(employeeDetails.getMonthlySalary() < 10000){
                 appraisal = 500;

              }else{
                 appraisal = 1000;
              }

              return appraisal;
           }



        }

4 个答案:

答案 0 :(得分:0)

没有被召唤。它就像是说你提供服务(在这种情况下,服务将是一个双倍),你想要付款(付款是EmployDetails)。提供EmployDetails,你给双倍。因此,当您想要调用calculateYearlySalary时,您提供了一个EmployDetails类的实例作为参数,它会返回(返回)double。

查看MethodsObjects,您可以在其中找到有关如何创建对象以及如何将这些对象“传递”到方法的更多信息。

答案 1 :(得分:0)

该行public double calculateYearlySalary(EmployeeDetails employeeDetails)是方法定义。

首先是访问修饰符public,第二个是返回类型double,然后是方法的名称。之后在括号中是参数。每个参数使用逗号分隔。 (例如public int foo(int first, double second, boolean third))。每个参数由两部分组成。首先是参数的类型(在您的示例中为EmployeeData,在我的示例中为intdoubleboolean),第二个是存储参数的变量的名称(在您的示例employeeDetails中,在我的示例中为firstsecondthird

答案 2 :(得分:0)

我会在EmployeeBusinessLogic中创建静态函数,因为业务逻辑似乎更多是实用函数。

此处的示例代码:

package general;

public class EmployeeBusinessLogic {
    // Calculate the yearly salary of employee
    public static double  calculateYearlySalary(EmployeeDetails employeeDetails) { //How and Why EmployeeDetails is taken here?
        double yearlySalary = 0;
        yearlySalary = employeeDetails.getMonthlySalary() * 12;
        return yearlySalary;
    }

    // Calculate the appraisal amount of employee
    public static double calculateAppraisal(EmployeeDetails employeeDetails){
        double appraisal = 0;
        if (employeeDetails.getMonthlySalary() < 10000) {
            appraisal = 500;
        } else {
            appraisal = 1000;
        }
        return appraisal;
    }

    public static void main(String[] args) {
        EmployeeDetails emp1 = new EmployeeDetails();
        emp1.setAge(30);
        emp1.setName("Rob Dylan");
        emp1.setMonthlySalary(4000);
        System.out.println("Annual salary of " + emp1.getName() + " is " + calculateYearlySalary(emp1));
    }
}

输出如下:

  

Rob Dylan的年薪是48000.0

答案 3 :(得分:0)

为什么:

这样您就可以使用EmployeeDetails类的方法了。

为了在EmpBusinessLogic类的方法中访问有关员工的信息,您需要传递EmployeeDetails对象,现在基于此对象,您可以计算该员工的年薪以及其他员工的年薪

如何: 首先,您创建EmployeeDetails类的对象并填写所有信息。

EmployeeDetails details=new EmployeeDetails()
details.setMonthlySalary(22222.22);

接下来你制作一个EmpBusinessLogic对象

现在,当您调用此类的方法时,您可以传递EmployeeDetails对象并获取工资详细信息和其他内容

EmpBusinessLogic logic=new EmpbusinessLogic();
System.out.println(logic.calculateYearlySalary(EmployeeDetails details));