[java] import j…
methodオーバーロード第2段
public class Rei17 {
    public static int mymax(int x, int y){
        return (x>y) ? x : y; //三項演算
    }
    public static double mymax(double x, double y){
        return (x>y) ? x : y;
    }
    public static String mymax(String x, String y){
        return (x.compareTo(y)>0) ? x : y;
    }
    public static void main(String[] args) {
        System.out.println(mymax(30,60));
        System.out.println(mymax(3.5,1.2));
        System.out.println(mymax("Rolla","Lisa"));
    }
}
オーバーロードはメソッドの機能追加なのね。上書きではないのね。 実行結果:
60 3.5 Rolla
compareTo()は、二つオブジェクトの値を比較するメソッド
 

ディスカッション
コメント一覧
まだ、コメントがありません