Java implements instance code for anonymous classes through interfaces

  • 2020-04-01 02:24:27
  • OfStack


package com.chase.test;

interface I1 {
    void print();
    void eat();
}
public class AnonymousClass {

    public static String excute(I1 c){
        System.err.println("excute Methods the print ...");
        c.print();
        c.eat();
        return "excute() The value returned ...";
    }

    public String testExcute() {
        return excute(new I1(){
                public void print() {
                    System.out.println(" Invokes anonymous internal printing ...");
                }
                public void eat() {
                    System.out.println(" The food was delicious today ...");
                }
            }) ;
        }

    public static void main(String[] args) {
        AnonymousClass entity = new AnonymousClass();
        System.out.println(entity.testExcute());
    }
}

Operation results:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201310/20131018171222408.gif" >


Related articles: