Last active
December 18, 2015 09:38
-
-
Save fge/5762353 to your computer and use it in GitHub Desktop.
Anonymous overloading in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.fge.uritemplate; | |
| public final class Test | |
| { | |
| public static class A | |
| { | |
| public static final int x = 1; | |
| public int m(A a, B b) | |
| { | |
| return a.m(b, b) + x; | |
| } | |
| } | |
| public static class B | |
| extends A | |
| { | |
| public int m(A a1, B a2) | |
| { | |
| if (a1 == a2) | |
| return x; | |
| return super.m(a1, a2); | |
| } | |
| } | |
| public static void main(final String... args) | |
| { | |
| B b1 = new B() | |
| { | |
| int m(B a, B b) | |
| { | |
| return 10; | |
| } | |
| }; | |
| System.out.println(b1.m(b1, b1)); // prints 1 | |
| System.out.println(new B() { | |
| int m(B a, B b) { return 10; } | |
| }.m(null, null)); // prints 10 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment