2017-04-11 11 views
0

別のフィールドの条件に基づいて1つのフィールドからデータを取り上げるにはどうすればよいですか?別のフィールドの条件に基づいて1つのフィールドからデータを取り上げる方法

以下に示すように、私はクラスの給料」からのデータとクラスのシミュレーション」を移入したい、条件は次のようである:

  1. simulation.id = salary.id。
  2. いくつかのidはsimulatedSalaryを持ち、いくつかのidはnotsimulatedSalaryを持ち、いくつかのidはsimulatedとnotsimulated 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(シミュレーション.SimulatedSalary +シミュレーション.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; 

}

+0

親クラスによってpublicまたはprotectedあるすべてのメソッドとフィールドを使用することができます。私は基本的なJavaチュートリアルを再訪する必要があると思います。 – satnam

+0

こんにちは@QSYこのWebサイトを使用してjavaの命名規則に従ってくださいhttp://www.oracle.com/technetwork/java/codeconventions-135099.html – abcOfJavaAndCPP

答えて

0

あなたは上記のコードを実装する場合、あなたはprotectedデータフィールドを使用する必要がありますまた、継承を使用する必要がありますし、両方のクラスにプライベートではありません

相続の

使用例:このコードでは、あなたの親クラスが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では複数の継承は許可されません。しかし、あなたは2つの親クラス以上を持つ 'interface'を作成することができます – abcOfJavaAndCPP