1 //匿名内部类实现方法
2 interface Dao {
3 public void add();
4 }
5
6 class Outer {
7 public void print() {
8 new Dao() {
9
10 @Override
11 public void add() {
12 // TODO Auto-generated method stub
13 System.out.println("添加学生");
14 }
15 }.add();
16 }
17 }
18
19 public class Demo4 {
20 public static void main(String[] args) {
21 Outer outer = new Outer();
22 outer.print();
23 }
24 }
1 /*
2 匿名对象
3 */
4
5 class Student
6 {
7 public static void student()
8 {
9 System.out.println("匿名对象输出测试");
10 }
11 }
12
13 class Drive
14 {
15 public static void drive(Student s)
16 {
17 s.student();
18 }
19 }
20 class noNameDemo
21 {
22 public static void main (String[] args)
23 {
24 new Drive().drive(new Student()); //只可单次使用
25 }
26 }