匿名内部类的简单使用
生活随笔
收集整理的这篇文章主要介绍了
匿名内部类的简单使用
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1. new 一个抽象的类:
package com.xuzhiwen.test1;abstract class Person {abstract void eat(); } package com.xuzhiwen.test1;public class TestPerson {public static void main(String[] args) {Person p = new Person() {@Overridevoid eat() {System.out.println("i can eat");}};p.eat();} }2. new 一个接口:
package com.xuzhiwen.test1;public interface Animal {public void run(); } package com.xuzhiwen.test1;public class Dog {public static void main(String[] args) {new Animal() {@Overridepublic void run() {System.out.println("run()..");}public void getName(){System.out.println("getName()...");}}.getName();} }
转载于:https://www.cnblogs.com/beibidewomen/p/7287674.html
总结
以上是生活随笔为你收集整理的匿名内部类的简单使用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 2983:谁是你的潜在朋友
- 下一篇: Navigator 对象