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 修饰符。

总结
今天团长给大家总结了一个接口的使用方法,后面在日常的开发中我们不单单只是在接口中定义抽象方法,也可以根据需求增加默认方法或者私有方法,以及在需要用到标记的时候也可以通过定义一个空接口来实现,怎么样是不是很棒,感觉动起来吧
Fantastic facts. Thank you!
learn to write essays how long are college essays
Position effectively taken!!
buy already written essays online best place to buy essay online
Cheers, I value it.
do my it homework should i do my homework
With thanks! I value this.
write my essay free online write a business proposal for me
Nicely put. Kudos.
juwa online casino download [url=https://bestonlinecasinoreal.us/]caesars online casino promo code[/url] ct online casino
Amazing postings, Cheers.
pay someone to do your essay
Valuable postings. Thank you.
write in cursive for me write my essay helper
Thanks a lot! Ample forum posts!
write my college admissions essay how to write an about me section for a website
This is nicely said. !
ideal essay writers top essay writers
Wow tons of good advice!
write an annotated bibliography for me what can i write my essay on
Truly lots of fantastic knowledge!
my homework do my finance homework
Cheers! An abundance of posts!
mba dissertation help editor for dissertation
Regards! Ample information.
write essay for money website that writes essays for you
Wonderful material, Kudos.
pay someone to write my paper how to write an abstract for a research paper
Really lots of amazing advice.
pay for paper pay someone to write your paper
Very good data. Thanks a lot.
online essay writing service best essay writing website
Cheers, A lot of write ups.
write a cover letter for me ai essay writer
Info certainly utilized!.
online essay writers online paper writers
Amazing quite a lot of terrific data.
college paper writer how to write a scientific paper
Amazing a good deal of amazing data!
buy literature review paper psychology paper writing service
Amazing loads of great data.
can i pay someone to write my essay essay buy
Cheers! Terrific stuff.
buy a custom essay buy cheap essays
You actually said it fantastically!
essay writing service buy last minute essay writing service
Appreciate it. Lots of write ups.
pay someone to write paper buy essays cheap
Terrific information. Many thanks.
cheap paper writing service best rated essay writing service
Amazing a good deal of valuable tips!
buy a paper college paper writing service
Very good data. Thanks a lot!
essay helper free best essay writing service
Amazing all kinds of amazing data.
do my homework do my college homework
Reliable information. Appreciate it!
write my essay for free website that writes essays for you
You actually stated this well.
pay for paper essay for sale
You actually mentioned this fantastically!
dissertation help what is a phd
Regards, I like this!
writing an expository essay essay writing sites
Nicely put. Kudos!
write paper research paper writer
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
Regards. Valuable information!
dissertation editing services dissertation service
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
Superb facts. Thanks.
essay writer review help me write my essay
You expressed this terrifically!
pay for research paper buying papers for college
Nicely put, Thanks a lot.
college term papers proposal writer
Very good write ups. Thank you!
website that writes essays for you professional essay writers
Awesome write ups. Kudos!
buy a paper custom research paper writing services
With thanks. Numerous information!
help me write my essay write this essay for me
Excellent forum posts. Many thanks.
write a research paper how to write a paper
Terrific posts. Appreciate it!
write my essays online write my essay
Terrific forum posts. Appreciate it!
essay writer free trial writing a personal essay
Nicely put, Regards.
research paper services research paper writer
Kudos, I value it.
do my essay free write my paper for me free
You’ve made your position quite nicely!!
do my essay write a essay for me
Point clearly taken!.
who can write my essay automatic essay writer
Reliable tips. Thanks.
coursework writing service essay writing paper
You have made your point.
write my research paper research paper writers
This is nicely put! .
cheap essay writing service us best online essay writing services reviews
Truly a good deal of excellent material!
pay someone to write paper where to buy essays online
Amazing lots of very good tips.
dissertation writing help dissertation abstract
Really a good deal of superb knowledge!
essay helper online essay helper
Tips certainly used!.
paper writing services pay someone to write your paper
Wow many of amazing knowledge.
paper writer online professional paper writers
Cheers. Lots of stuff.
proposal research research proposals
You mentioned this terrifically!
are essay writing services safe online check writing service
Fantastic posts. Regards.
persuasive essay writer writers of the federalist papers
Incredible all kinds of wonderful info.
fast essay writing service best online essay writing services reviews
With thanks! Ample material!
write paper for me my paper writer
You explained it fantastically.
pay for essay papers essay for sale
Awesome forum posts. Kudos!
paper writers paper writing
Superb forum posts. Appreciate it!
buy term papers online proposal writer
Nicely put. Regards!
smart writing service easy essay writing
Thanks a lot! Ample information.
what is a claim in essay writing monster resume writing service discount
You stated it very well!
professional paper writing service personal essay writing service
Very good content. Cheers.
doctoral thesis argumentative thesis
Well voiced really. .
phd dissertation help dissertation meaning
coursework writing service writing essays service
how to write an essay for college admission
Nicely put. Cheers!
buy a paper for college custom handwriting paper
Great content. Regards!
i need help writing an essay writing services for college papers
dissertation writing dissertation writing service
learning to write essays
Thanks, Lots of material.
essay writers online do my essay
Really loads of good material!
writing a good essay do my essay
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
Many thanks! I enjoy it.
write a paper persuasive essay writer
Truly plenty of good advice!
order essay essay for sale
Many thanks! Good information.
do my math homework do my homework for free
do my essay free professional essay writers
essay writing service us
You said it nicely..
pay someone to write my paper writing papers
You expressed it perfectly!
definition of dissertation dissertation service
You definitely made your point!
essay paper writing service college essay writing
Perfectly spoken genuinely! !
custom papers best paper writing services
dissertation editing dissertation writing services
help me write a essay
Lovely posts. Regards.
dissertation abstract dissertation writer
You said it very well..
dissertations dissertation help services
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
Wow quite a lot of awesome tips.
pro essay writing service writing an essay introduction
Amazing data, Appreciate it!
dissertation service phd dissertation
Fantastic stuff. Cheers.
cheap assignment writing service unique essay writing service
custom essay writing service professional essay writing service
college goals essay
Nicely put, Thank you!
academic essay writing service my essay service
Reliable material. Thanks a lot.
good thesis statements doctoral thesis
Effectively voiced of course. !
argumentative thesis thesis writing service
essay helper online free writing assistant
essay helper app
Whoa plenty of great information.
proposal introduction research proposals
Nicely put, Thanks!
dissertation assistance dissertation data analysis help
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
You said it perfectly..
research paper helper cheap research paper writing service
You actually suggested it terrifically.
medical school personal statement medical school personal statement
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
Kudos, Numerous posts!
do my college homework hire someone to do my homework
proposal writer thesis proposal
mba essay editing service
Wonderful postings. Thank you.
essay writing help assignment help
Tips certainly regarded!!
homework help cpm do my math homework
Fine information. Thank you.
thesis writing service writing a thesis statement
argumentative essay writing helper
how to write a personal biography essay
Awesome content. Thanks!
thesis writing thesis sentence
Superb content. Thanks.
write my essay service writing a narrative essay
Very well voiced indeed. !
online paper writing service paper writing services
cheapest essay writing service writing a college application essay
umi dissertation express
Thank you! I appreciate it.
pay for paper order essay online
Very good information. Cheers!
argumentative thesis thesis statement
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
Many thanks, A lot of information.
buy college essays pay for college papers
Nicely put. Thank you.
essay writing best essay writing service reddit
strong thesis statement define thesis
professional cv writing service
Seriously lots of awesome data.
college paper writing service buy college paper
This is nicely put. .
definition of dissertation definition of dissertation