@Override 注解用于当开发人员重写 Java 中的函数以使用相同的函数名称但为这些函数分配不同的属性时。如果您知道 Java 中的过度函数,但尚未使用 @override 注释,因为您不想将其用作显式编写它的强制选项。自 Java 1.5 引入以来,它默认处于激活状态。它促进运行时多态性。这是因为我们可以在不使用注释的情况下重写任何函数。尽管如此,它还是有一个主要优点:如果编译器偶然错过了重写(就像开发人员在重写函数名称中犯了拼写错误)。在重写注释的帮助下,编译器将理解并用子函数重写基函数。它还提高了代码的可读性,减少了维护时间和精力。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
public @interface Override
符号“@”应该出现在覆盖关键字之前,以便编译器了解这是否是注释。重写函数在基类和继承类中应具有相同的定义以及返回类型和多个参数。如果其中任何一个存在差异,则不会将其视为覆盖函数,同时将此函数理解为新函数。
@Override 注解在 Java 中如何工作?
- 重写注释在继承类中定义的重写函数之前使用,以确保编译器理解该函数是故意在两个不同的类中使用相同的参数和返回类型定义的。
- 为了让系统了解要调用哪个函数,因为该函数在基类和继承类中具有相同的参数,我们必须使用实例来调用该函数。
- 如果使用父类的对象调用函数,则调用父类函数及其本地函数定义,而如果使用继承类的对象,则调用继承类的函数。
示例:
Base class {} Child class{} Base object1= new Base();// Here Base class is instantiated so the function definition will be called from base class. Base object2= new Child(); /// Here Child class is instantiated so the function definition will be called from child class
- 现在上面定义的名为“object1”和“object2”的对象与点运算符一起使用来提取函数定义。
- 所有这些函数只有在代码实际执行开始的主类下维护时才有效。
- 控件将命中主类,然后搜索主类上方预定义的类的对象实例。
- 然后该对象将调用被调用的函数。
- 如果已经在类定义中定义为原型,也可以在函数中传递所需的参数。
- 一个基类可以被多个类继承;唯一的区别是对象创建和使用该对象调用函数。
Java @Override 示例
以下是示例:
示例#1
演示覆盖注释工作原理的示例。
说明:
下面的程序中定义了两个类:一个是基类,也称为父类“Pclass”,另一个类“Cclass”,继承了基类的属性和成员函数,是称为继承类或子类。该函数首先在父类中声明。在此示例中,函数名称为 printfunction(),它负责打印作为参数传递的字符串。
在名为“Cclass”的继承类中声明和定义同名函数,并在其前面带有 @override 注解。其他字符串作为参数传递给它。在主类中,上面定义的类通过创建它们的对象来实例化。 “object1”标识Pclass 的对象,“object2”标识Cclass 的对象。使用这些不同的对象调用相同的函数。在第一种情况下,object1 从父类 Pclass 获取字符串。而稍后,当调用 object2 时,@override 注释就会起作用并更改内容字符串。这是 Java 下提供的一个重要工具,用于实现易于理解的代码和更好的功能。
代码:
// This is Base class class Pclass { void printfunction() { System.out.println("This is the output of function present in parent class \"Pclass\". "); } } // This is Child class class Cclass extends Pclass { // The below function is override function along with override annotation @Override void printfunction() { System.out.println("This is the output of function present in child class \"Cclass\"."); } } // Thi is Main class from here the contro; execution starts. JAVA compiler searches for main class to start executing any code. public class Main { public static void main(String[] args) { Pclass object1 = new Pclass(); object1.printfunction(); Pclass object2 = new Cclass(); object2.printfunction(); } }
输出:
这是带有两行字符串的输出屏幕。第一个字符串行来自基函数,而第二个字符串行来自继承类中定义的重写函数。
Example #2
Here we have one base class with two child classes inheriting it. The second inherited class is instantiated, and the output string is triggered from the 2nd inherited class.
Code:
class Pclass { void printfunction() { System.out.println("This is the output of function present in parent class \"Pclass\". "); } } // This is Child class class Cclass extends Pclass { // The below function is override function along with override annotation @Override void printfunction() { System.out.println("This is the output of function present in child class \"Cclass\"."); } } // This is Child class class Cclass2 extends Pclass { // The below function is override function along with override annotation @Override void printfunction() { System.out.println("This is the output of function present in child class number 2 \"Cclass\"."); } } // This is Main class from here the contro; execution starts. JAVA compiler searches for main class to start executing any code. public class Main { public static void main(String[] args) { Pclass object1 = new Pclass(); object1.printfunction(); Pclass object2 = new Cclass2(); object2.printfunction(); } }
Output:
Conclusion
Hence Java override function comes with a lot of benefits like providing run-time polymorphism, easy code access, clean code and many more. Adding override annotation assures that the compiler understands the intention of function definition via function declarations in classes. This is one of the important properties of the oops concept called polymorphism.
以上是Java @Override的详细内容。更多信息请关注PHP中文网其他相关文章!

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

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

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

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1
功能强大的PHP集成开发环境

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

Atom编辑器mac版下载
最流行的的开源编辑器