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
你new一个接口类是必须实现接口的方法。
匿名类 Comparator<T> comp = new Comparator<T>() { @Override public int compare(T o1, T o2) { // TODO Auto-generated method stub return 0; } }; 要实现接口中的方法
实例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
Comparator<T> comp = new Comparator<T>() { @Override
public int compare(T o1, T o2) {
// TODO Auto-generated method stub
return 0;
}
};
要实现接口中的方法
}大括号里面是不是还自己实现了某些方法咯,这不就是一个实体类了吗,只是这个类没有名字,就直接被你new出对象了,用接口的类型也就是父类类型指向子类对象,也可以说是多态。
你去看一下匿名类就应该有所了解了。