黑马程序员技术交流社区

标题: 匿名内部类的一些简单总结 [打印本页]

作者: a80194367    时间: 2015-7-5 08:32
标题: 匿名内部类的一些简单总结
匿名内部类总结及例子
匿名内部类也就是没有名字的内部类

正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写

但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口



实例1:不使用匿名内部类来实现抽象方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
abstract class Person {
    public abstract void eat();
}

class Child extends Person {
    public void eat() {
        System.out.println("eat something");
    }
}

public class Demo {
    public static void main(String[] args) {
        Person p = new Child();
        p.eat();
    }
}
运行结果:eat something

可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Person类的引用

但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?

这个时候就引入了匿名内部类



实例2:匿名内部类的基本实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
abstract class Person {
    public abstract void eat();
}

public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}
运行结果:eat something

可以看到,我们直接将抽象类Person中的方法在大括号中实现了

这样便可以省略一个类的书写

并且,匿名内部类还能用于接口上


实例3:在接口上使用匿名内部类
interface Person {
    public void eat();
}

public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}
运行结果:eat something



由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口



实例4:Thread类的匿名内部类实现
public class Demo {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        };
        t.start();
    }
}
运行结果:1 2 3 4 5



实例5:Runnable接口的匿名内部类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Demo {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }
}
运行结果:1 2 3 4 5


作者: 430422    时间: 2015-7-5 08:42
此贴不火,非人道也!
作者: 帅帅loyal    时间: 2015-7-5 08:52
最近刚好学到线程这里,谢谢分享
作者: 等你的季节-夏天    时间: 2015-7-5 10:15
谢谢分享
作者: dajiaoya    时间: 2015-7-5 13:16
分享快乐
作者: a80194367    时间: 2015-7-5 18:22
帅帅loyal 发表于 2015-7-5 08:52
最近刚好学到线程这里,谢谢分享

线程有难度啊,表示现在还有一些不懂,细节
作者: 帅帅loyal    时间: 2015-7-5 19:26
a80194367 发表于 2015-7-5 18:22
线程有难度啊,表示现在还有一些不懂,细节

写的多了就会了,,我现在正在看第二遍呢,果然温故而知新
作者: keto    时间: 2015-7-5 19:33
谢谢分享,学习了。。。。
作者: a80194367    时间: 2015-7-5 20:17
帅帅loyal 发表于 2015-7-5 19:26
写的多了就会了,,我现在正在看第二遍呢,果然温故而知新

你说的对
作者: firwood    时间: 2015-7-5 20:55
总结的很好,例子也明白。
作者: 形而上孤独    时间: 2015-7-5 20:57
顶一个!
作者: wuwenwen3779    时间: 2015-7-5 22:15
点个赞喽!!!!!!!
作者: hejin67410    时间: 2015-9-21 20:35
楼主,谢谢分享




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