在 Java 8 中,引入了一个名为默认方法的新概念,以实现通过旧接口控制 lambda 表达式的向后兼容性。此外,接口允许具有可实现的功能,而不会对将要实现接口的类造成任何问题。在引入 Java 8 之前,接口仅允许抽象方法。此外,必须在不同的类中提供功能。在以下部分中,将解释默认方法的语法、工作原理和示例。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
以下是默认方法的语法。
public interface animal { default void sound() { System.out.println("This is a sample default method. . .!"); }}
默认方法在 Java 中如何工作?
众所周知,List、集合等接口没有forEach方法。如果添加它,集合的框架实现将会中断。由于Java 8中引入了默认方法,因此可以对方法forEach进行默认实现。除此之外,一个类可以实现两个具有相同默认功能的接口。让我们看看代码的歧义是如何解决的。
public interface livingthings { default void sound() { . . . System.out.println("Living things too make noise . . .") ; } } public interface animals { default void sound() { . . . System.out.println("animals too make noise . . .") ; } }
对于这种歧义有两种解决方案。
1. 要覆盖默认方法实现,请创建您自己的方法。
public class dogs implements livingthings,animals { default void sound() { . . . System.out.println("dogs bark . . .") ;} }
2.使用super,调用默认方法
public class dogs implements livingthings,animals { default void sound() { . . . livingthings.super.print("dogs bark . . .") ; } }
在Java中,通常一个类可以实现n个接口。此外,一个接口可以通过另一个接口进行扩展。假设一个类有两个接口,并且实现了一个默认方法;特定的类可能会在选择考虑调用哪个方法时感到困惑。为了解决这些冲突,可以做到以下几点。
- 匹配并调用类中的重写方法。
- 如果提供了相同方法,将选择最合适的方法。假设有两个接口,A 和 B 分别针对特定类。如果A扩展了B,那么A在这里是最具体的,默认方法将从A中选择。如果A和b是独立的接口,就会发生严重的冲突情况,编译器会抱怨它是无法选择。在这种情况下,用户必须通过提供额外的详细信息来帮助编译器,从中选择默认方法 A 或 B。例如
A.super.demo() ;
或
B.super.demo() ;
普通方法和默认方法的区别
现在,让我们看看普通方法和默认方法之间的一些区别
- 与普通方法不同,默认方法带有默认修饰符。
- 接口的参数对于默认方法没有任何特定状态。
- 普通方法可以使用以及更改方法和类字段的参数。
- 可以将新功能添加到现有接口中,而不是破坏这些特定接口的先前实现。
在扩展包含默认方法的接口时,可以执行以下操作。
- 默认方法不会被覆盖,并且会被继承。
- 默认方法将被重写,这与子类中重写的方法类似。
- 默认方法应再次声明为抽象方法,这会强制子类被重写。
实现 Java 默认方法的示例
以下是提到的示例程序:
示例#1
实现默认方法的Java程序
代码:
//Java program to implement default method public class DefExample { //main method public static void main(String args[]) { //create an object for the interface animal Animals obj = new Dog(); //call the print method obj.print( ); } } //create an interface animal interface Animals { //default method default void print( ) { System.out.println("I have four legs. . . ! !"); } static void sound() { System.out.println("I used to bark alot!!!") ; } } //create an interface <u>carnivores</u> interface Carnivores { //default method default void print( ) { System.out.println("I eat non veg. . .! !") ; } } //class that implements the other two interfaces class Dog implements Animals, Carnivores { public void print( ) { //call the print method of Animals using super Animals.super.print( ) ; //call the print method of Carnivores using super Carnivores.super.print( ); //call the sound method of Animals Animals.sound(); System.out.println("I am a Dog . . .!"); } }
输出:
说明:在这个程序中,Animals 和 Carnivores 两个接口有相同的默认方法 print(),并且使用 super 调用。
示例#2
实现默认方法的Java程序
代码:
//Java program to implement default method interface Sampleinterface{ // Since this is declared as a default method, this has to be implemented in the implementation classes default void sammethod(){ System.out.println("a default method which is newly added to the program"); } // existing public as well as abstract methods has to be implemented in the implementation classes void oldmethod(String s); } public class DefExample implements Sampleinterface{ // abstract method implementation public void oldmethod(String s){ System.out.println("The string given is: "+ s); } public static void main(String[] args) { DefExample obj = new DefExample(); //call the default method obj.sammethod(); //call the abstract method obj.oldmethod("I am the old method in the program"); } }
输出:
说明:在这个程序中,存在一个接口Sampleinterface,它有一个默认方法sammethod(),并且被调用。
结论
在 Java 8 中,提供了一个名为默认方法的新概念,用于在旧接口控制 lambda 表达式的情况下执行向后兼容性。此外,接口参数对于默认方法没有任何特定状态。在本文中,详细解释了默认方法的语法、工作原理和示例。
以上是Java默认方法的详细内容。更多信息请关注PHP中文网其他相关文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6
视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)