zoukankan      html  css  js  c++  java
  • Java接口与实例化

    看代码看到

    public Runnable r = new Runnable()
    {
    @Override
    public void run()
    {
       ...
    }
    }

    接口不能new ,不过可以生成一个匿名类,省略了写一个具体类实现接口的开销。

    public class Main {
    
        public static void main(String[] args)  {
            String a=new CustomerImpl().sayHello("hongda");
            System.out.println(a);
            String b=new Customer(){
                public String sayHello(String name){
                    return "Hello2 "+name;
                }
            }.sayHello("hongdada");
            System.out.println(b);
    
        }
    }
    
    interface Customer {
        public String sayHello(String name);
    }
    
    class CustomerImpl implements Customer {
        @Override
        public String sayHello(String name) {
            return "Hello, " + name;
        }
    }
    Hello, hongda
    Hello2 hongdada

    如果接口内有多个方法呢?

    public class Main {
    
        public static void main(String[] args)  {
            String a=new CustomerImpl().sayHello("hongda");
            System.out.println(a);
            String b=new Customer(){
                public String sayHello(String name){
                    return "Hello2 "+name;
                }
            }.sayHello("hongdada");
            System.out.println(b);
    
        }
    }
    
    interface Customer {
        public String sayHello(String name);
        public int Add(int a ,int b);
    }
    
    class CustomerImpl implements Customer {
        @Override
        public String sayHello(String name) {
            return "Hello, " + name;
        }
        @Override
        public int Add(int a ,int b){
            return a+b;
        }
    }
    Error:(8, 32) java: <匿名com.company.Main$1>不是抽象的, 并且未覆盖com.company.Customer中的抽象方法Add(int,int)

    匿名类内部实现接口全部方法:

    public class Main {
    
        public static void main(String[] args)  {
            String a=new CustomerImpl().sayHello("hongda");
            System.out.println(a);
            String b=new Customer(){
                public String sayHello(String name){
                    return "Hello2 "+name;
                }
                public int Add(int a ,int b){
                    return a+b;
                }
            }.sayHello("hongdada");
            System.out.println(b);
    
        }
    }
    
    interface Customer {
        public String sayHello(String name);
        public int Add(int a ,int b);
    }
    
    class CustomerImpl implements Customer {
        @Override
        public String sayHello(String name) {
            return "Hello, " + name;
        }
        @Override
        public int Add(int a ,int b){
            return a+b;
        }
    }
    Hello, hongda
    Hello2 hongdada

    这种实现接口方式其实就是一个实现一个继承接口的匿名类。

    http://blog.sina.com.cn/s/blog_70441c8e0101sgnf.html

  • 相关阅读:
    SAP分析云及协同计划
    使用SSH命令行远程登录运行在CloudFoundry上的应用
    如何远程调试部署在CloudFoundry平台上的nodejs应用
    Apache httpclient的execute方法调试
    如何用Java代码在SAP Marketing Cloud里创建contact数据
    nodejs request module里的json参数的一个坑
    如何在调用Marketing Cloud contact创建API时增加对扩展字段的支持
    Efim and Strange Grade
    Vitya in the Countryside
    Anatoly and Cockroaches
  • 原文地址:https://www.cnblogs.com/hongdada/p/6144044.html
Copyright © 2011-2022 走看看