小高分享(54)Java中的super关键字和final关键字

createh54个月前 (12-29)技术教程45

分享兴趣,传播快乐,

增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为你带来

小高分享(54)Java中的super关键字和final关键字

欢迎您的访问!

Share interests, spread happiness,

increase knowledge, and leave beautiful!

Dear you, this is LearningYard Academy.

Today, the editor brings you

Xiao Gao shares (54) the super keyword and final keyword in Java

welcome your visit!


1. super关键字详解

1. Description of the super keyword

(1)继承的魅力:super关键字用于表示父类(超类)的引用。在继承关系中,子类可以通过super关键字调用父类的方法和属性。

(1) The charm of inheritance: The super keyword is used to represent a reference to the parent class (superclass). In an inheritance relationship, a subclass can invoke the methods and properties of its parent class with the super keyword.

(2)调用父类构造方法:在子类的构造方法中,使用super()可以调用父类的构造方法。如果不显式调用,Java默认会在子类构造方法的第一行调用父类的无参构造方法。

(2) Call the superclass constructor: In the constructor of the subclass, use super() to call the constructor of the superclass. If not explicitly called, Java defaults to calling the parent class's parameterless constructor on the first line of the subclass constructor.

(3)解决同名问题:当子类和父类中存在同名方法或属性时,使用super关键字可以明确指出调用的是父类的方法或属性,避免混淆。

(3) Solve the problem of the same name: when there are methods or attributes of the same name in the subclass and the parent class, the super keyword can clearly indicate that the method or attribute of the parent class is called to avoid confusion.

(4)super与this的区别:super用于指向父类,而this指向当前对象。在构造方法中,super()和this()都不能同时出现,因为它们都必须位于第一行。

(4) The difference between super and this: super is used to point to the parent class, while this refers to the current object. In a constructor, neither super() nor this() can appear together, because they must both be on the first line.

2. final关键字详解

2. Description of the final keyword

(1)不可更改的类:使用final修饰的类称为最终类,不能被其他类继承。例如,Java中的String类就是一个最终类,保证了字符串操作的安全性。

(1) Unalterable classes: Classes that use final are called final classes and cannot be inherited by other classes. For example, the String class in Java is a final class that guarantees the security of string operations.

(2)不可重写的方法:final修饰的方法称为最终方法,不能被子类重写。这有助于保证方法的行为在继承体系中保持不变。

(2) Non-overridden methods: final modified methods are called final methods and cannot be overridden by subclasses. This helps ensure that the behavior of the method remains unchanged in the inheritance system.

(3)常量定义:final修饰的变量称为常量,其值一旦赋值,就不能更改。常量名通常使用全大写字母表示,如:public static final int MAX_VALUE = 100;

(3) Constant definition: The final variable is called a constant, and its value cannot be changed once it is assigned. Constant names are usually represented in all uppercase letters, for example: public static final int MAX_VALUE = 100;

(4)提高性能:在Java中,使用final关键字可以提升性能。编译器会尝试内联所有final方法,减少方法调用的开销。

(4) Improve performance: In Java, using the final keyword can improve performance. The compiler attempts to inline all final methods, reducing the overhead of method calls.

(5)final与static结合:final和static关键字可以一起使用,表示一个类的常量,即静态常量。例如:public static final int PI = 3.14159;

(5) final combined with static: The final and static keywords can be used together to represent a constant of a class, that is, a static constant. For example, public static final int PI = 3.14159;

3. super与final的应用

3. super and final applications

(1)在继承关系中,合理使用super关键字,可以更好地复用父类的代码,提高开发效率。

(1) In the inheritance relationship, the rational use of the super keyword can better reuse the code of the parent class and improve development efficiency.

(2)在设计类时,考虑使用final关键字,确保类的安全性。例如,将工具类设置为final,防止他人意外继承。

(2) When designing the class, consider using the final keyword to ensure the security of the class. For example, setting the tool class to final prevents others from accidentally inheriting it.

(3)在方法设计上,使用final关键字可以避免子类修改方法的行为,保证方法的稳定性。

(3) In method design, using the final keyword can avoid the behavior of subclasses to modify the method and ensure the stability of the method.

(4)利用final变量定义常量,提高代码的可读性和可维护性。

(4) Use final variables to define constants to improve the readability and maintainability of the code.

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天,

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article,

Welcome to leave us a message,

Let us meet tomorrow,

I wish you a nice day today!

相关文章

三十四、Java中的final关键字 java中final关键字的用途

Java中的final关键字是一种修饰符,它有着多种用途,主要应用在变量、方法和类上,以指示不可变性或不可覆盖性。final 关键字修饰不同元素的作用Java元素作用变量当final修饰基本类型变量时...

Java 详细剖析关键字 static,深入全面了解

1. 概述static 是一种修饰符static 是Java中表静态的关键字它可以修饰成员变量、成员方法、代码块被static修饰的成员变量或成员方法,将不再依赖于对象的创建而去使用,而是依赖类的存在...

长知识了!Java 关键字 transient 还能这么用

前言最近在看 HashMap 源代码的时候,发现链表 table 数组采用了transient 关键字,笔者当时感觉对 transient 关键字即陌生但又有似曾相识,所以花了一些时间简要的总结了下使...

《Java基础知识》Java断言 - - 关键字 assert

背景断言:也就是所谓的assertion,是jdk1.4后加入的新功能。 它主要使用在代码开发和测试时期,用于对某些关键数据的判断,如果这个关键数据不是你程序所预期的数据,程序就提出警告或退出。 当软...

java基础之static关键字 java 中static

我真是个莫得感情的更新机器关于static的使用,之前学习接触过的场景如下:静态修饰符,(作用于变量,方法,代码块)有位大神说过:“static方法就是没有this的方法。在static方法内部不能调...

面试官:说一下 final 关键字和 final 的 4 种用法?

重要说明:本篇为博主《面试题精选-基础篇》系列中的一篇,查看系列面试文章请关注我。Gitee 开源地址:https://gitee.com/mydb/interviewfinal 定义final 翻译...