// リスト 4 // 四則演算クラス // ファイル: Addition2.java // Addision2クラス public class Addition2 { // フィールドの定義 private int num1; private int num2; public int ans1 = 0; public double ans2 = 0.0; // メソッドの定義 // 変数代入メソッド void substituteValue(int a, int b) { // 同じオブジェクト内のフィールドへの代入 num1 = a; num2 = b; } // 足し算メソッド(1): 引数は2つのint型 int addParameter(int a, int b) { // 同じオブジェクト内のメソッドの呼び出し substituteValue(a, b); // 同じオブジェクト内のフィールドの参照・代入 ans1 = num1 + num2; // 戻り値 return ans1; } // 足し算メソッド(2): 名称は(1)と同じで,引数の数が異なる(3つのint型) int addParameter(int a, int b, int c) { // メソッドのオーバーロード // 戻り値は,引数をすべて足した値 ans1 = a + b + c; return ans1; } // 足し算メソッド(3): 名称は(1)と同じで,引数の型が異なる(2つのdouble型) double addParameter(double a, double b) { // メソッドのオーバーロード // 戻り値は,引数をすべて足した値 ans2 = a + b; return ans2; } } // Addition2クラス