网站首页 > 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!
猜你喜欢
- 2025-01-02 Netty 断开重连
- 2025-01-02 “算法考核没过,老板找我聊了3个小时”:离职程序员重刷数学题
- 2025-01-02 List 去除重复数据的五种方式,舒服
- 2025-01-02 你真的会用MySQL去重吗?看看DISTINCT和GROUP BY的奥秘
- 2025-01-02 Java8两个List集合取交集、并集、差集、去重并集
- 2025-01-02 干货实战~Java如何防止接口重复提交
- 2025-01-02 面试突击63:MySQL 中如何去重?
- 2025-01-02 Java中List集合对象去重及按属性去重的8种方法
- 2025-01-02 Java实现10万+并发去重,持续优化
- 2025-01-02 java接口防重提交如何处理
你 发表评论:
欢迎- 04-24Java Collections 工具类集合框架中常用算法解析
- 04-24桶排序的简单理解
- 04-24Java集合框架底层实现原理大揭秘
- 04-24Java 集合框架全面解析:选对数据结构,提升开发效率
- 04-24c#集合排序
- 04-24Java面试中常被问到的集合类深度解读
- 04-24VBA技术资料MF278:对集合进行排序
- 04-24Spring 最常用的 7 大类注解,史上最强整理
- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)