电信的电箱烧了,害我断了2天网,真拿命,耽误了
Java匿名内部类的总结:
没有名字的内部类。就是内部类的简化形式。一般只用一次就可以用这种形式。匿名内部类其实就是一个匿名子类对象。想要定义匿名内部类:需要前提,内部类必须继承一个类或者实现接口。
package day10; abstract class Fu { public abstract void run(); } class Zi extends Fu { public void run() { System.out.println("run...."); } } public class AnonymousClass01 { public static void main(String[] args) { Zi p = new Zi(); p.run(); } }运行结果:
package day10; abstract class Fu { public abstract void run(); } public class AnonymousClass01 { public static void main(String[] args) { new Fu(){ public void run(){ System.out.println("run...."); } }.run(); } }运行结果:
package day10; interface Fu { public abstract void run(); } public class AnonymousClass01 { public static void main(String[] args) { new Fu(){ public void run(){ System.out.println("run...."); } }.run(); } }运行结果:
<span style="font-size:18px;">package day10; interface Fu { public abstract void run(); public abstract void sleep(); } public class AnonymousClass01 { public static void main(String[] args) { Fu x = new Fu(){ public void run(){ System.out.println("run...."); } public void sleep(){ System.out.println("sleep...."); } }; x.run(); x.sleep(); } }</span>运行结果:
package day10; interface Fu { public abstract Fu run(); public abstract void sleep(); } public class AnonymousClass01 { public static void main(String[] args) { new Fu(){ public Fu run(){ System.out.println("run...."); return this; } public void sleep(){ System.out.println("sleep...."); } }.run().sleep(); } }运行结果: