「Java」オーバーロード(Overload)を使うサンプル
構文
オーバーロード(Overload)
関数A(int,String)
関数A(int,int,String)
メソッド名は同じでも引数が異なります。
Javaコード
package com.arkgame.study.cft;
public class KdfcSample {
// メソット1 int型パラメータ 2つ
public void funcA(int x, int y) {
int result = x + y;
System.out.println("メソット1の結果: " + result);
}
// メソット2 int型パラメータ2つ string型パラメータ1つ
public void funcA(int x, int y, String z) {
int res = Integer.valueOf(z).intValue();
int result = x + y + res;
System.out.println("メソット2の結果: " + result);
}
// メソット3 int型パラメータ1つ String型パラメータ2つ
public void funcA(int x, String y, String z) {
int res1 = Integer.valueOf(y).intValue();
int res2 = Integer.valueOf(z).intValue();
int result = x + res1 + res2;
System.out.println("メソット3の結果: " + result);
}
}
確認実行クラス
package com.arkgame.study.cft;
public class OverLoadResu {
public static void main(String[] args) {
KdfcSample cft = new KdfcSample();
// メソッド1を呼び出す
cft.funcA(5, 6);
// メソッド2を呼び出す
cft.funcA(5, 6, "7");
// メソッド3を呼び出す
cft.funcA(5, "6", "7");
}
}
実行結果
メソット1の結果: 11
メソット2の結果: 18
メソット3の結果: 18