首页 > 其他 > 详细

Inner Class & Anonymity

时间:2020-07-07 13:25:22      阅读:55      评论:0      收藏:0      [点我收藏+]

匿名子类 对 抽象类 而言, 匿名实现类 对 接口 而言

匿名子类

// Father.java
public abstract class Father {
    public abstract  void say();
}
// Say.java
public class Say {
    public void say(Father father){
        father.say();
    }
}
// Client.java
public class Client {
    public static void main(String[] args) {
        Say say = new Say();
        say.say(new Father() {
            @Override
            public void say() {
                System.out.println("I am the father.");
            }
        });
    }
}

匿名实现类

// Father.java    Here is the change
public interface Father {
    void say();
}
// Say.java
public class Say {
    public void say(Father father){
        father.say();
    }
}
// Client.java
public class Client {
    public static void main(String[] args) {
        Say say = new Say();
        say.say(new Father() {   // @Override is not allowed in implement
            public void say() {
                System.out.println("Hi, this is Tom.");
            }
        });
    }
}

Inner Class & Anonymity

原文:https://www.cnblogs.com/nedrain/p/13260066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!