2017-04-11 81 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

相關問題