黑马程序员技术交流社区

标题: 匿名内部类的部分实现 [打印本页]

作者: ringfingers    时间: 2015-10-10 23:50
标题: 匿名内部类的部分实现
实例4:Thread类的匿名内部类实现[backcolor=rgb(255, 255, 255) !important][size=1em]
[backcolor=rgb(244, 244, 244) !important]public class Demo {
    public static void main(String[] args) {
[backcolor=rgb(244, 244, 244) !important]        Thread t = new Thread() {
            public void run() {
[backcolor=rgb(244, 244, 244) !important]                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
[backcolor=rgb(244, 244, 244) !important]                }
            }
[backcolor=rgb(244, 244, 244) !important]        };
        t.start();
[backcolor=rgb(244, 244, 244) !important]    }
}



运行结果:1 2 3 4 5


实例5:Runnable接口的匿名内部类实现[backcolor=rgb(255, 255, 255) !important][size=1em]
[backcolor=rgb(244, 244, 244) !important]1

2

[backcolor=rgb(244, 244, 244) !important]3

4

[backcolor=rgb(244, 244, 244) !important]5

6

[backcolor=rgb(244, 244, 244) !important]7

8

[backcolor=rgb(244, 244, 244) !important]9

10

[backcolor=rgb(244, 244, 244) !important]11

12

[backcolor=rgb(244, 244, 244) !important]13

[backcolor=rgb(244, 244, 244) !important]public class Demo {
    public static void main(String[] args) {
[backcolor=rgb(244, 244, 244) !important]        Runnable r = new Runnable() {
            public void run() {
[backcolor=rgb(244, 244, 244) !important]                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
[backcolor=rgb(244, 244, 244) !important]                }
            }
[backcolor=rgb(244, 244, 244) !important]        };
        Thread t = new Thread(r);
[backcolor=rgb(244, 244, 244) !important]        t.start();
    }
[backcolor=rgb(244, 244, 244) !important]}





作者: ringfingers    时间: 2015-10-10 23:51
======




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2