专业的JAVA编程教程与资源

网站首页 > java教程 正文

小高分享(53)Java中成员变量的隐藏和方法重写

temp10 2025-01-02 19:14:30 java教程 11 ℃ 0 评论


小高分享(53)Java中成员变量的隐藏和方法重写

分享兴趣,传播快乐,

增长见闻,留下美好!

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

今天小编为你带来

小高分享(53)Java中成员变量的隐藏和方法重写

欢迎您的访问!

Share interests, spread happiness,

increase knowledge, and leave beautiful!

Dear you, this is LearningYard Academy.

Today, the editor brings you

Xiao Gao Shares (53) Hiding and method rewriting of member variables in Java

welcome your visit!

一、成员变量的隐藏

First, hiding of member variables

1. 概念:成员变量的隐藏是指子类中声明了一个与父类同名的成员变量,导致父类的同名成员变量在子类中不可见。

1. Concept: The hiding of member variables means that a member variable with the same name as the parent class is declared in the subclass, resulting in the parent class with the same name member variable is not visible in the subclass.

2. 特点:

2. Features:

隐藏发生在同一作用域内,即子类和父类具有相同的成员变量名。

Hiding occurs in the same scope, that is, the subclass and the parent class have the same member variable name.

隐藏不会改变父类成员变量的值,只是在子类中无法直接访问。

Hiding does not change the value of the parent class member variable, it is just not directly accessible in the subclass.

子类可以通过使用super关键字来访问被隐藏的父类成员变量。

Subclasses can access hidden parent class member variables by using the super keyword.

3. 隐藏的本质:成员变量的隐藏是一种名称遮蔽现象,它不涉及方法体的覆盖,仅仅是变量名的覆盖。

3. The nature of hiding: The hiding of member variables is a name masking phenomenon, which does not involve the coverage of the method body, but only the coverage of the variable name.

二、方法重写

Second, method rewriting

1. 概念:方法重写是指子类中声明了一个与父类同名、同参数列表、同返回值类型的方法,从而覆盖了父类中的同名方法。

1. Concept: Method rewriting means that a subclass declares a method with the same name as the parent class, the same parameter list, and the same return value type, thereby overwriting the method with the same name in the parent class.

2. 特点:

2. Features:

方法重写要求子类方法的访问权限不低于父类方法的访问权限。

Method rewriting requires that the access rights of the subclass methods are no less than the access rights of the parent class methods.

子类方法必须与父类方法具有相同的方法签名(方法名和参数列表)。

The subclass method must have the same method signature (method name and argument list) as the parent class method.

子类方法可以通过super关键字调用父类中被覆盖的方法。

A subclass method can use the super keyword to call a method that is overridden in the parent class.

方法重写是多态性的体现,允许子类在继承父类方法的基础上,实现特定的功能。

Method overwriting is the embodiment of polymorphism, allowing subclasses to implement specific functions on the basis of inheriting parent methods.

3. 重写的本质:方法重写是子类对父类方法的重新实现,它保持了原有方法的名称和参数列表,但可以有不同的实现逻辑。

3. The essence of rewriting: Method rewriting is the re-implementation of the parent class method by the subclass, which maintains the name and parameter list of the original method, but can have different implementation logic.

三、成员变量隐藏与方法重写的区别

Third, the difference between member variable hiding and method rewriting

1. 目的:成员变量隐藏是为了在子类中使用与父类同名的变量,而方法重写是为了在子类中实现与父类同名方法的不同功能。

1. Purpose: Member variable hiding is to use variables with the same name as the parent class in the subclass, and method rewriting is to implement different functions in the subclass with the same name as the parent class.

2. 影响范围:成员变量隐藏仅影响变量本身,而方法重写影响的是整个方法体。

2. Scope of influence: Member variable hiding only affects the variable itself, while method rewriting affects the whole method body.

3. 多态性:成员变量隐藏不涉及多态性,而方法重写是多态性的关键实现方式。

3. Polymorphism: Member variable hiding does not involve polymorphism, and method rewriting is a key way to achieve polymorphism.

4. 访问权限:成员变量隐藏不要求子类变量的访问权限与父类变量相同,但方法重写要求子类方法的访问权限不低于父类方法。

4. Access rights: Member variable hiding does not require that the access rights of subclass variables are the same as that of parent class variables, but method rewriting requires that the access rights of subclass methods are not lower than that of parent class methods.

四、总结

Fourth, summary

成员变量的隐藏和方法重写是Java面向对象编程中不可或缺的两个特性。它们在继承关系中起着重要作用,但各自有不同的应用场景和目的。了解和掌握这两者的区别,有助于我们更好地运用Java的继承机制,提高代码的可读性、可维护性和复用性。

The hiding of member variables and method rewriting are two essential features of Java object-oriented programming. They play an important role in inheritance relationships, but each has a different application scenario and purpose. Understanding and mastering the difference between the two will help us make better use of Java's inheritance mechanism and improve the readability, maintainability and reusability of 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!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表