Created
October 19, 2011 18:26
-
-
Save nissuk/1299208 to your computer and use it in GitHub Desktop.
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
import java.util.List; | |
import java.util.ArrayList; | |
// http://d.hatena.ne.jp/t_yano/20080622/1214087678 | |
public class Example { | |
public static void main(String[] args) { | |
List<Person> list = new ArrayList<Person>() {{ | |
add(new Person(){{ name = "foo"; age = 20; }}); | |
}}; | |
for (Person p : list) { | |
System.out.printf("%s (%d)", p.name, p.age); | |
} | |
} | |
} |
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
public class Person { | |
public String name; | |
public int age; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment