2017-04-11 85 views
0

如何根据另一个字段的条件从一个字段获取数据?如何根据另一个字段的条件从一个字段获取数据

如下所示,我要填充与来自类的工资“数据的类“仿真”,条件是这样的:

  1. simulation.id = salary.id;
  2. 一些id有模拟沙拉,有的id没有模拟沙拉,有的id有模拟和未模拟的Salary;
  3. if(salary.simulated == true)then simulation.simulatedSalary = salary.salary,else simulation.simulatedSalary == 0;
  4. if(salary.simulated == false)then simulation.notsimulatedSalary = salary.salary,else simulation.notsimulatedSalary == 0;
  5. simulation.totalSalary = sum(simulation.simulatedSalary + simulation.notsimulatedSalary)。

如何实现上述条件?

List<salary> salaryList填充List<simulation> simulationList

public class salary { 
private Integer id; 
private Boolean simulated; 
private Double salary; 

}

public class simulation { 
private Integer id; 
private Double simulatedSalary; 
private Double notsimulatedSalary; 
private Double totalSalary; 

}

如果你想实现你应该使用继承你也应该使用 protected数据字段上面的代码
+0

这个代码是没有意义的publicprotected领域。我认为你需要重新学习一个基本的Java教程。 – satnam

+0

嗨@QSY请遵循java的命名约定,使用这个网站http://www.oracle.com/technetwork/java/codeconventions-135099.html – abcOfJavaAndCPP

回答

0

和对您的班级不是私人的

遗产的样本用法:在这段代码中,你的父类是Salary和你的子类是Simulation则必须将你的数据字段为protected,而不是private,这样子类可以使用它的父类变量直接

在继承子类可以使用所有的方法和由父类

public class Salary 
    { 
     protected Integer id; 
     protected Boolean simulated; 
     protected Double salary; 

    } 

public class Simulation extends Salary 
{ 
//you do not need Integer id here 
private Double simulatedSalary; 
private Double notsimulatedSalary; 
private Double totalSalary; 
} 
+0

谢谢,我按照你的建议来做这样的逻辑:public void setSimSalary( Double simSalary){this.simSalary = super.getSimulated()。equals(“Y”)? this.simSalary = super.getSalary():0; } – QSY

+0

@QSY使用'protected'变量意味着当您创建一个Salary对象时,对象无法直接访问其变量,只有Salary的子类可以直接访问Salary变量 – abcOfJavaAndCPP

+0

@QSY请注意,Java中不允许多重继承但你可以创建一个'interface'来拥有2个父类或更多 – abcOfJavaAndCPP

相关问题