JDK1.8之后的新特性和新接口 接口的旧特性: 接口的新特性: 非静态的非抽象方法: 静态方法: 私有方法:

就特性下接口中只有:
常量(必须赋值)
抽象方法abstract(和final static private三个关键字冲突)

interface Inter {
    //int a ; //编译报错 常量必须要赋值不能使用默认值。
    int a = 10; // 前面有默认修饰符 public static final
    //void show(){}  //编译报错 抽象方法必须没有方法体
    void show(); //前面有默认修饰符 public abstract
}

接口的新特性:

新特性在旧特性的基础上新加了

非抽象方法

interface Inter {
    //非抽象方法  必须给出 default 关键字
    public default void method1(){ // 你给了default 前面就不会再有 默认修饰符 public abstract			
    }
}

静态方法

interface Inter {
    //静态方法
    public static void method2(){// 你给了static 前面就不会再有 默认修饰符 public abstract	

    }
}

静态私有方法

interface Inter {
    //静态私有方法
    private static void method2(){// 你给了static 前面就不会再有 默认修饰符 public

    }
}

非静态私有方法。

interface Inter {
    //非静态私有方法。
    private void method1(){ // 你给了private 前面就不会再有 默认修饰符 public abstract			
    }
}

非静态的非抽象方法:

它的出现解决接口升级问题。1万个类实现了一个接口,这时候对接口进行了升级,按照jdk1.7的规则,加方法的话只能加
抽象方法,当加完抽象方法之后1万个类瞬间编译报错。因为必须要重写抽象方法。
有的时候我们希望1万个类如果有类想升级那么重写,有类的不想升级就别重写了。这时候default来了。

案例1:

interface Inter {
    public default void show(){
        System.out.println("show");
    }
}
class Student implements Inter {

}
public class Test {
    public static void main(String[] args){
        //Inter i = new Inter(); //编译报错。 接口就算是jdk1.8也不能创建对象啊。
        //i.show(); 

        //Inter.show(); // 编译报错。因为default方法不是静态不能用类名调用.
        // default 就不是给你接口准备的, 

        Student s = new Student();
        s.show();

        Inter i  = new Student();
        i.show();  // 多态除了 重写的方法 是调用的子类之外, 其他的所有的东西 都是调用的父类  父类没有那就只能报错了。
    }
}

案例2:

interface Inter {
    default void show(){  //前面不写public的话  默认有
        System.out.println("show");
    }
}
class Student implements Inter {
    void show(){  // 编译报错  子类重写重写父类的方法 权限修饰符 必须大于等于父类的权限修饰符
        System.out.println("zi show");
    }
}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.show(); // 
    }
}

案例3:

interface Inter1 {
    void method();
    public default void show(){
        System.out.println("show1");
    }
}
interface Inter2 {
    void method();
    public default void show(){
        System.out.println("show2");
    }
}
class Student implements Inter1,Inter2 {  // 多实现的规则 继续沿用。 但是 规定了 当一个类实现了多个接口,而多个接口中出现了 一样的 default方法, 那么子类必须重写,谁都不用
    public void method(){  // 这也是 接口可以多实现的原因
        System.out.println("method");
    }
    public void show(){
        System.out.println(" zi show");
    }
}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.method(); // method
        s.show(); //zi show
    }
}

静态方法:

作用:让接口具备了功能, 让接口来调用。

案例1:

interface Inter {
    public static void show(){   // 
        System.out.println("show");
    }
}
class Student implements Inter {

}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.show(); // 编译报错。 接口的静态方法 不是给儿子准备的。 儿子用不了。

        Inter.show();
    }
}

案例2:

class Person {
    public static void show(){   // 
        System.out.println("show");
    }
}
class Student extends  Person {

}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.show();  // show  正确的,  类的静态方法, 子类是可以调用的。
    }
}

案例3:

class Person {
    public static void show(){   // 
        System.out.println("fu show");
    }
}
class Student extends  Person {
    public static void show(){   // 
        System.out.println("zi show");
    }
}
public class Test {
    public static void main(String[] args){
        Person p = new Student();
        p.show();  // zi show   静态的方法 不存在重写, 是谁的 就是谁的。
    }
}

案例4:

interface Inter1 {
    void method();
    public static void show(){
        System.out.println("show1");
    }
}
interface Inter2 {
    void method();
    public static void show(){
        System.out.println("show2");
    }
}
class Student implements Inter1,Inter2 {
    public void method(){  // 这也是 接口可以多实现的原因
        System.out.println("method");
    }

    // Student 作为接口的子类  是无法使用接口的静态方法的, 所以  多实现接口的时候 随便有多少个相同的静态方法。
}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.method(); // method
        s.show(); //编译报错					
    }
}

私有方法:

可以把多个非静态的非抽象方法 或者 多个静态方法 的共性的内容 抽取成一些静态方法。

interface Inter {
    public static void show1(){
        show3();
    }
    public static void show2(){
        show3();
    }
    private static void show3(){
        System.out.println("show");
        System.out.println("show");
    }

    public default void method1(){
        method3();
        show3();
    }
    public default void method2(){
        method3();
        show3();
    }
    //private default void method3(){ // 编译报错。 你只要写了default 前面绝对默认是 public的 改不了。 你却private 冲突了。
    //void method3(){ //  编译报错。 啥都不写 前面默认是public abstract的
    private void method3(){ // 你写了 private之后  就不再有 publica bstract了 也不再有default了
        System.out.println("method");
        System.out.println("method");
    }
}
class Student implements Inter {

}
public class Test {
    public static void main(String[] args){
        Student s = new Student();
        s.method3();// 编译报错。 私有的
        Inter.show3(); // 编译报错。 私有的
    }
}