java8的interface和abstract class

来源:互联网 发布:空间数据的逻辑运算 编辑:程序博客网 时间:2024/06/07 07:23

一:java8接口interface的变化

1. 在java8中接口可以有default方法和static方法。java8之前的接口中的方法全部默认为 public abstract method_name(),变量全部默认为public static final

default method in interface 默认方法

简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法。只需在方法名前面加个default关键字即可。 

interface Drawable{  void draw();  default public void msg(){System.out.println("default method");}  }  class Rectangle implements Drawable{  public void draw(){System.out.println("drawing rectangle");}  }  class TestInterfaceDefault{  public static void main(String args[]){  Drawable d=new Rectangle();  d.draw();  d.msg();  }}  

default方法是提交提供的一个默认实现,也可以像其他方法一样被继承。接口可以有多继承,当多个父接口中有相同签名的默认方法时,子实现类中需要以

<Interface接口名>.super.<method方法名>()的形式指定要调用哪个父接口中的方法

由于同一个方法可以从不同接口引入,自然而然的会有冲突的现象,默认方法判断冲突的规则如下:

1.一个声明在类里面的方法优先于任何默认方法(classes always win)

2.否则,则会优先选取最具体的实现,比如下面的例子 B重写了A的toOverride方法。

interface A{    void foo();    default void toOverride() {        System.out.println("A");    }}interface B extends A{    default void toOverride() {        A.super.toOverride();        System.out.println("B");    }}interface C extends B{    default void toOverride() {        A.super.toOverride();// does not work        B.super.toOverride();        System.out.println("B");    }}class D implements B{    public void toOverride() {    }        public void foo() {        D.this.toOverride();        B.super.toOverride();        A.super.toOverride(); // does not work!    }}


static method in interface  通过接口名就可以直接调用其静态方法

interface Drawable{  void draw();  static int cube(int x){return x*x*x;}  }  class Rectangle implements Drawable{  public void draw(){System.out.println("drawing rectangle");}  }    class TestInterfaceStatic{  public static void main(String args[]){  Drawable d=new Rectangle();  d.draw();  System.out.println(Drawable.cube(3));  }}  


二:接口interface和抽象类abstract class的区别

Abstract classInterface1) Abstract class can have abstract and non-abstractmethods.Interface can have only abstract methods. Since Java 8, it can have default and static methods also.2) Abstract class doesn't support multiple inheritance.Interface supports multiple inheritance.3) Abstract class can have final, non-final, static and non-static variables.Interface has only static and final variables.4) Abstract class can provide the implementation of interface.Interface can't provide the implementation of abstract class.5) The abstract keyword is used to declare abstract class.The interface keyword is used to declare interface.6) Example:
public abstract class Shape{
public abstract void draw();
}Example:
public interface Drawable{
void draw();

7)接口中没有 this 指针,没有构造函数. 抽象类中可以有 

注意:

  • default 关键字只能在接口中使用(以及用在 switch 语句的 default 分支),不能用在抽象类中。

  • 接口默认方法不能覆写 Object 类的 equalshashCode 和 toString 方法。

  • 接口中的静态方法必须是 public 的,public 修饰符可以省略,static 修饰符不能省略。


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 错误码2611003怎么办 优惠券被天猫吞了怎么办 鞋子放鞋盒发霉怎么办 蘑菇街登不上怎么办 桑蚕丝衣服皱褶怎么办 羊毛衫会扎人怎么办 肌肉腿外翻怎么办 大腿赘肉怎么办 淘宝卖家不举证怎么办 咸鱼收到假货怎么办 伤没钱看病怎么办 steam付款失败怎么办 配偶没有公积金怎么办 玩吃鸡配置不够怎么办 华为AL20黑屏怎么办 华为照片不见了怎么办 电话卡暂停服务怎么办 电脑屏幕居中了怎么办 冰箱霜太多怎么办 qq账号忘了怎么办没有邮箱怎么办 车祸赔不起怎么办对方天天闹怎么办 专家解读有口臭怎么办口气重怎么办 油锅起火时应该怎么办或者怎么办 淘宝买了东西想改地址怎么办怎么办 手机显示程序异常这是怎么办怎么办 钢笔笔尖坏了怎么办 陶瓷刀钝了怎么办 地下城fps低怎么办 床头只能朝西怎么办 钢笔笔尖歪了怎么办 钢笔笔尖漏墨怎么办 被蜈蚣咬了怎么办 信用卡被盗刷了怎么办 练太极拳膝盖痛怎么办 僵尸毛毛虫咬了怎么办 迅雷下载速度慢怎么办 驾驶证换证过期怎么办 发生地震时该怎么办 尿常规潜血十3怎么办 尿酮体弱阳性怎么办 蔡英文当选大陆怎么办