Interface implemented in two ways (complementary interfaces knowledge)

1, explicit implementation

interface Runner{
    public void run();
}
class Kid implements Runner{
    @Override
    public void run() {
        System.out.println("小孩在疯跑!!!");
    }
}
public class InterfaceDemo1 {
    public static void main(String[] args){
        Kid kid=new Kid();
        kid.run();
    }
}

result:

Child Fengpao! ! !

2, implicitly implement

interface Runner2{
    public void run2();
}
public class InterfaceDemo2 {
    public static void main(String[] args){
        Runner2 r1=new Runner2() {
            @Override
            public void run2() {
                System.out.println("我会跑步!!!");
            }
        };
        r1.run();

    }
}

result:

I will run! ! !

or:

nterface Runner2{
    public void run2();
}
public class InterfaceDemo2 {
    public static void main(String[] args){
        Runner2 r2= () -> System.out.println("我在学走路");
        r2.run2();

    }
}

result:

I'm learning to walk

 

发布了45 篇原创文章 · 获赞 8 · 访问量 5841

Guess you like

Origin blog.csdn.net/wenyunick/article/details/104344056