1. 程式人生 > >Java還要再學一遍基礎(四)JDK1.8新特性default,static

Java還要再學一遍基礎(四)JDK1.8新特性default,static

JDK1.8新特性default,static用法

 在1.8以前,我們的Interface之中通常除了抽象方法別的什麼都沒有,但是從1.8引入開始Interface中還可以有具體的實現!其中所要用到的兩個非常重要的關鍵字就是:default和static

default修飾的預設方法,可以帶有具體的實現,同時這個介面的實現類可以不去實現這個方法就能夠使用,當然也可以自己去實現這個方法(如果有必要的話)。
例:
UserService介面

public interface UserService {
    String saySomething();

    default
String sayHello(){ return "ok I just say hello"; }; }

UserServiceImpl實現:

public class UserServiceImpl implements UserService{

    @Override
    public String saySomething() {
        return "I have nothing to say";
    }

    public static void main(String args[]){
        UserService user = new
UserServiceImpl(); System.out.println(user.saySomething()); System.out.println(user.sayHello()); } }

執行後輸出結果:

I have nothing to say
ok I just say hello

同樣也可以去實現這個預設方法:

public class UserServiceImpl implements UserService{

    @Override
    public String saySomething() {
        return
"I have nothing to say"; } @Override public String sayHello(){ return "I want to say something more..."; } public static void main(String args[]){ UserService user = new UserServiceImpl(); System.out.println(user.saySomething()); System.out.println(user.sayHello()); } }

執行結果:

I have nothing to say
I want to say something more...

呼叫的是子類的實現。

static的使用:

UserService介面:

public interface UserService {
    String saySomething();

    default String sayHello(){
        return "ok I just say hello";
    };

    static String staticFunction(){
        return "this is a inteface static function";
    }
}

UserServiceImpl實現:

public class UserServiceImpl implements UserService{

    @Override
    public String saySomething() {
        return "I have nothing to say";
    }

    @Override
    public String sayHello(){
        return "I want to say something more...";
    }
    public static void main(String args[]){
        UserService user = new UserServiceImpl();
        System.out.println(user.saySomething());
        System.out.println(user.sayHello());
        System.out.println(UserService.staticFunction());
    }
}

執行結果:

I have nothing to say
I want to say something more...
this is a inteface static function

根平常的static方法呼叫一樣。
但是當一個類實現了兩個介面,這兩個介面中有相同名字的預設方法,將會報編譯錯誤。

補充:
既然可以有static方法,而且介面預設方法是public的,那麼是不是可以在介面中定義main方法來執行呢?
實驗:

public interface UserService {

    String saySomething();

    default String sayHello(){
        return "ok I just say hello";
    };

    static String staticFunction(){
        return "this is a inteface static function";
    }

    static void main(String args[]){
        System.out.println("inter face run");
    }
}

結果:執行成功,輸出:

inter face run

同時在實現類中也同樣可以呼叫:

public class UserServiceImpl implements UserService{

    @Override
    public String saySomething() {
        return "I have nothing to say";
    }

    @Override
    public String sayHello(){
        return "I want to say something more...";
    }
    public static void main(String args[]){
        UserService user = new UserServiceImpl();
        System.out.println(user.saySomething());
        System.out.println(user.sayHello());
        System.out.println(UserService.staticFunction());
        UserService.main(null);
    }
}

執行結果:

I have nothing to say
I want to say something more...
this is a inteface static function
inter face run

JDK 1.8中介面和抽象類的區別

由於介面引入了介面和靜態方法,這裡就很容易聯想到抽象類。好像兩個的功能基本一樣。但是其實也有不同。
  1. 一個類可以實現多個介面,但最多隻能實現一個抽象類。
  2. 介面中的例項變數是預設是final的,並且只能用public,static,final修飾,static而抽象類中的例項變數則沒有這以要求。
  3. 介面中的方法預設都是public的,而抽象類中的方法可以規定不同的作用域。
  4. 介面的實現必須實現所有的抽象方法(預設方法除外),而子類繼承自抽象類可以不用實現所有的抽象方法。
  5. 介面中不能有構造方法,抽象類中可以有構造方法。
  6. 從設計層面上來講:介面是對行為的規範,用於描述行為的一種抽象,而抽象類是對類的抽象,是一種模板式的設計,同時抽象類的繼承含有a is b的意義在裡面。

區別雖然多,但是似乎對於用起來很容易用混,到底用什麼我覺得還是取決於:1)設計層面上到底是對於類的抽象還是行為的規範。2)是否需要考慮到作用域的問題。