LoginSignup
3
0

More than 5 years have passed since last update.

Java でもa == 1 && a == 2 && a == 3でtrueって出力させたい(魔術でもなんでもない王道編)

Posted at

ただのOverrideです。
さっき書いた↓のコードはProxyで「動的」に処理を変えてますが、こっちは「静的」になっただけです。

Java でもa == 1 && a == 2 && a == 3でtrueって出力させたい(黒魔術ってほどでもないグレーな魔術編) - Qiita

public class JudgeImpl2 {
    public static void main(String... args) throws Exception {
        {
            System.out.println("まずは普通に実行すると・・・");
            JudgeImpl2 judge = new JudgeImpl2();
            test(judge);
        }
        {
            System.out.println("上書きすると・・・");
            JudgeImpl2 judge = new JudgeImpl2() {
                @Override
                public boolean judge(int a ) {
                    return true;
                }
            };
            test(judge);
        }
    }

    private static void test(JudgeImpl2 judge){
        System.out.println( judge.judge(1) );
        System.out.println( judge.judge(2) );
        System.out.println( judge.judge(3) );
    }

    public boolean judge(int a) {
        if (a == 1 && a == 2 && a == 3) {
            return true;
        } else {
            return false;
        }
    }
}

実行結果

まずは普通に実行すると・・・
false
false
false
上書きすると・・・
true
true
true
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0