電信的電箱燒了,害我斷了2天網,真拿命,耽誤了html
Java匿名內部類的總結:java
沒有名字的內部類。就是內部類的簡化形式。通常只用一次就能夠用這種形式。匿名內部類其實就是一個匿名子類對象。想要定義匿名內部類:須要前提,內部類必須繼承一個類或者實現接口。this
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(); } }運行結果: