匿名内部类
匿名内部类:
1、匿名内部类其实就是内部类的简写格式
2、匿名内部类必须要继承父类或者实现接口时才能使用
3、匿名内部类的格式:new 父类或者接口(){定义子类的内容}
继承父类
abstract class AnonyClass{
<span style="white-space:pre"> </span>abstract void show();
}
class Test11{
<span style="white-space:pre"> </span>public static void main(String[] args){
<span style="white-space:pre"> </span>new AnonyClass(){
<span style="white-space:pre"> </span>void show(){
<span style="white-space:pre"> </span>System.out.println("呵呵");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}.show();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
interface AnonyClass{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>void show();
}
class AnonyInter{
<span style="white-space:pre"> </span> static class ShowAnonyInter implements AnonyClass{
<span style="white-space:pre"> </span> public void show(){
<span style="white-space:pre"> </span> System.out.println("嘻嘻");
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
}
class Test11{
<span style="white-space:pre"> </span>public static void main(String[] args){
<span style="white-space:pre"> </span>new AnonyInter.ShowAnonyInter().show();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}