您的位置 首页 java

Java 中间的接口还可以这样用,你知道吗?

java 程序员 都知道要面向接口编程,那 Java ​ 中的接口除了定义接口方法之外还能怎么用你知道吗?今天团长就来带大家看一下 Java 中间的接口还可以有哪些用法。

基本特性

我们先看一下接口的基本特性

  • 接口的定义需要使用关键字interface;
  • 接口定义的所有方法默认都是public abstract
  • 当一个具体的class​ 去实现一个interface​时,需要使用implements 关键字;
  • 接口之间是可以多继承,而类是只能单继承的;

如下所示,我们定义一个接口;

 package com.example.demo.inter;

import java.io.Serializable;
import java.util.RandomAccess;


public interface ITest  extends  Serializable, RandomAccess {

  public abstract String sayHello();
   String  sayHello2();

}  

默认方法

在 JDK 8​ 之前接口是不支持默认方法的,在 JDK 8​ 之后接口支持默认方法,默认方法采用关键词 default 声明。

 public interface ITest extends Serializable, RandomAccess {

  public abstract String sayHello();
  String sayHello2();
  
  
  default String sayHello3() {
    return "hello3";
  }

}  

默认方法跟抽象方法不一样,接口中定义的抽象方法,当接口被其他类实现的时候都需要全部实现,但是默认方法是不需要被实现就可以直接使用的,类似于直接调用父类的方法一样,所以在很多时候,我们已经继承了一个类,还想有一个能用但是不想每个子类都实现的方法的时候,就可以考虑增加一个接口的默认方法来使用,简单来说就是实现类可以不覆写 default 方法。

default方法存在的目的是,在我们已经完善的项目中,如果我们直接给一个接口增加一个方法,在没有默认方法的时候就需要给所有的实现类都实现对应的方法,但是这个方法又不一定是每个实现类都需要的,所以这个时候默认方法就很好的解决了这个问题,我们只需要增加一个默认方法,然后在需要使用的实现类中进行实现或者使用就可以了,其他的实现类不需要改动任何的代码。

标记

接口还有一个很常见的功能那就是标记功能,这么说可能你没有印象,但是提到 序列化 接口 java.io.Serializable;​ 你肯定知道,我们经常在对应的 POJO 中都会实现这个序列化接口,而这个序列化的接口如果看过源码的小伙伴肯定知道里面是没有内容的。

同样的功能除了序列化的接口,类似的空间接口还有很多,比如 java.util.RandomAccess​ 也是空间接口,之前阿粉也写过关于 RandomAccess​ 这个接口的用途,感兴趣的可以再去看看。RandomAccess 明明是个空接口,能有什么用呢?

通过源码我们可以知道 RandomAccess 是用来标识子类是否实现了该接口,如果实现了则走实现了的逻辑,没有实现就走没有实现的逻辑,所以我们在日常开发中也可以利用这个特性,当我们有不同的子类需要根据情况进行不同的实现逻辑的时候就可以采用定义一个空接口来标记一下,方便后面的处理。

静态方法

跟默认方法一样,JDK 8 还支持在接口中增加静态方法,虽然说在接口中定义静态方法的做法不常见,但是当需要使用的时候也是可以支持的,避免在创建一个单独的工具类,跟在类中定义的静态方法一样,我们可以直接通过接口名称引用静态方法,当然 静态变量 也是可以的,使用方法一样。

 public interface ITest extends Serializable, RandomAccess {

  public abstract String sayHello();
  String sayHello2();
  default String sayHello3() {
    System.out.println(sayHello4());
    return "hello3";
  }
  public  static  String sayHello4() {
    return "hello4";
  }
}  

私有方法

大家有没有注意到,上面不管是默认方法还是静态方法其实都是 public​ 公开的,可以让实现类或者其他类直接使用,那有没有可能在接口中定义一个私有方法呢?在 JDK 9​ 之前是不可以的,JDK 9 却支持了,如下所示

  private   void  privateMethod() {
    System.out.println("private私有方法被调用");
  }

  private static void privateStaticMethod() {
    System.out.println("private私有静态方法被调用");
  }  

从官方的文档中我们可以找到下面的描述,在 JDK 9 中接口支持私有方法,主要用于不同的非抽象方法中共享代码。

我们可以看到在 JDK 9 这样写是可以的

但是在 JDK8​ 就不行了,是无法编译通过的,会提示不允许使用 private 修饰符。

总结

今天团长给大家总结了一个接口的使用方法,后面在日常的开发中我们不单单只是在接口中定义抽象方法,也可以根据需求增加默认方法或者私有方法,以及在需要用到标记的时候也可以通过定义一个空接口来实现,怎么样是不是很棒,感觉动起来吧

文章来源:智云一二三科技

文章标题:Java 中间的接口还可以这样用,你知道吗?

文章地址:https://www.zhihuclub.com/184288.shtml

关于作者: 智云科技

热门文章

评论已关闭

111条评论

  1. Position effectively taken!!
    buy already written essays online best place to buy essay online

  2. With thanks! I value this.
    write my essay free online write a business proposal for me

  3. Nicely put. Kudos.
    juwa online casino download [url=https://bestonlinecasinoreal.us/]caesars online casino promo code[/url] ct online casino

  4. Thanks a lot! Ample forum posts!
    write my college admissions essay how to write an about me section for a website

  5. Wow tons of good advice!
    write an annotated bibliography for me what can i write my essay on

  6. Wonderful material, Kudos.
    pay someone to write my paper how to write an abstract for a research paper

  7. Very good data. Thanks a lot.
    online essay writing service best essay writing website

  8. Amazing quite a lot of terrific data.
    college paper writer how to write a scientific paper

  9. Amazing a good deal of amazing data!
    buy literature review paper psychology paper writing service

  10. You actually said it fantastically!
    essay writing service buy last minute essay writing service

  11. Terrific information. Many thanks.
    cheap paper writing service best rated essay writing service

  12. Reliable information. Appreciate it!
    write my essay for free website that writes essays for you

  13. Fine material. Cheers.
    [url=https://topswritingservices.com/]best resume writing service[/url] professional essay writers [url=https://essaywriting4you.com/]writing services for college papers[/url] fast essay writing service

  14. Kudos! Numerous advice!
    [url=https://essaywritingservicehelp.com/]essay service[/url] essay paper writing services [url=https://essaywritingservicebbc.com/]best assignment writing service[/url] college essay writing help

  15. You expressed this terrifically!
    pay for research paper buying papers for college

  16. Very good write ups. Thank you!
    website that writes essays for you professional essay writers

  17. With thanks. Numerous information!
    help me write my essay write this essay for me

  18. Terrific forum posts. Appreciate it!
    essay writer free trial writing a personal essay

  19. This is nicely put! .
    cheap essay writing service us best online essay writing services reviews

  20. Truly a good deal of excellent material!
    pay someone to write paper where to buy essays online

  21. You mentioned this terrifically!
    are essay writing services safe online check writing service

  22. Incredible all kinds of wonderful info.
    fast essay writing service best online essay writing services reviews

  23. Thanks a lot! Ample information.
    what is a claim in essay writing monster resume writing service discount

  24. You stated it very well!
    professional paper writing service personal essay writing service

  25. Well voiced really. .
    phd dissertation help dissertation meaning
    coursework writing service writing essays service
    how to write an essay for college admission

  26. Great content. Regards!
    i need help writing an essay writing services for college papers
    dissertation writing dissertation writing service
    learning to write essays

  27. Good info, Regards.
    pay for an essay pay for an essay
    [url=https://helpwithdissertationwriting.com/]dissertation help[/url] dissertation editing [url=https://dissertationwritingtops.com/]dissertation abstracts[/url] dissertation help
    graduation thesis

  28. Many thanks! Good information.
    do my math homework do my homework for free
    do my essay free professional essay writers
    essay writing service us

  29. Perfectly spoken genuinely! !
    custom papers best paper writing services
    dissertation editing dissertation writing services
    help me write a essay

  30. Incredible all kinds of good facts!
    will writing service writing essays services
    buy a research paper research proposal cover page
    best cv writing service in dubai

  31. Wow quite a lot of awesome tips.
    pro essay writing service writing an essay introduction

  32. Fantastic stuff. Cheers.
    cheap assignment writing service unique essay writing service
    custom essay writing service professional essay writing service
    college goals essay

  33. Effectively voiced of course. !
    argumentative thesis thesis writing service
    essay helper online free writing assistant
    essay helper app

  34. Seriously a lot of amazing material!
    paper help pay someone to write a paper
    do my homework do my programming homework
    writing a dissertation prospectus

  35. You actually suggested it terrifically.
    medical school personal statement medical school personal statement

  36. Cheers. I appreciate it!
    pay to do my homework do my math homework for me
    how to write a reaction paper my paper writer
    how to write a good essay introduction

  37. Kudos, Numerous posts!
    do my college homework hire someone to do my homework
    proposal writer thesis proposal
    mba essay editing service

  38. Fine information. Thank you.
    thesis writing service writing a thesis statement
    argumentative essay writing helper
    how to write a personal biography essay

  39. Very well voiced indeed. !
    online paper writing service paper writing services
    cheapest essay writing service writing a college application essay
    umi dissertation express

  40. You’ve made your point very well!!
    reddit do my homework do my homework for free
    research paper writer cheap research paper writing service
    dissertation wiki

  41. Nicely put. Thank you.
    essay writing best essay writing service reddit
    strong thesis statement define thesis
    professional cv writing service

网站地图