SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.ArrayList; | |
| 2 | ||
| 3 | public class Test {
| |
| 4 | ||
| 5 | public static void main(String[] args) {
| |
| 6 | new Test(); | |
| 7 | } | |
| 8 | ||
| 9 | ArrayList<Button> list = new ArrayList<Button>(); | |
| 10 | ||
| 11 | int i; | |
| 12 | ||
| 13 | Test() {
| |
| 14 | for (i = 0; i < 10; i++) {
| |
| 15 | Button b = new Button(i); | |
| 16 | b.l = new Listener() {
| |
| 17 | @Override | |
| 18 | public void trigger() {
| |
| 19 | System.out.println(i + ""); | |
| 20 | } | |
| 21 | }; | |
| 22 | list.add(b); | |
| 23 | } | |
| 24 | ||
| 25 | for (Button b : list) {
| |
| 26 | b.l.trigger(); | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | class Button {
| |
| 31 | int i; | |
| 32 | Listener l; | |
| 33 | ||
| 34 | Button(int i) {
| |
| 35 | this.i = i; | |
| 36 | } | |
| 37 | ||
| 38 | public void setListener(Listener l) {
| |
| 39 | this.l = l; | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | interface Listener {
| |
| 44 | public void trigger(); | |
| 45 | } | |
| 46 | } |