码迷,mamicode.com
首页 > 其他好文 > 详细

Inner Class & Anonymity

时间:2020-07-07 13:14:04      阅读:35      评论:0      收藏:0      [点我收藏+]

标签:static   lang   int   rgs   抽象   public   abstract   override   实现   

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

匿名子类

// 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

标签:static   lang   int   rgs   抽象   public   abstract   override   实现   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!