对于父类的私有属性,子类是从哪里访问到的?
来源:互联网 发布:js通过标签名获取元素 编辑:程序博客网 时间:2024/05/02 03:53
其实也是牵扯到子类继承父类时,父类的private属性在子类中是什么样的问题。
根据JAVA官方的定义:
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
其运行结果如下:
根据JAVA官方的定义:
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
子类是无法继承父类的private成员的,但是在子类对象的内存里有没有父类的private成员呢?来看以下的代码:
package cn.haiyisoft3;public class ExtendsDemo02 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// 验证子类能否访问父类中的private属性或者方法// 或者说继承?child ch = new child();ch.setage(10);ch.name = "xiaomim";System.out.println(ch);System.out.println(ch.getage());}}class person_private {private int age;String name;public void setage(int age) {this.age = age;}public int getage() {System.out.println(this);return this.age;}}class child extends person_private {void cry() {System.out.println("wowo");}}
其运行结果如下:
cn.haiyisoft3.child@55f33675cn.haiyisoft3.child@55f3367510这说明this和ch指向的是同一个对象,也就是说子类调用的自己实例对象内存空间的父类私有属性age。
所以,我认为子类实例空间中是有父类的私有成员的,但是有不代表继承,只有能直接使用才说明子类继承了父类。
0 0
- 对于父类的私有属性,子类是从哪里访问到的?
- JAVA在子类中访问父类的私有属性
- C++从子类访问父类的私有函数
- C++从子类访问父类的私有函数
- 从子类访问父类的私有函数
- C++:从子类访问父类的私有函数
- 子类会继承父类的私有方法和私有属性,只是不能直接访问
- GObject 子类私有属性的外部访问
- GObject 子类私有属性的外部访问
- 子类继承父类的私有属性
- 子类继承父类的私有属性
- 子类继承父类的私有属性
- 子类无权访问父类的私有数据成员
- 关于Java子类访问父类的私有变量
- 子类能否继承、访问父类的私有成员
- java 子类有没有继承父类的私有属性和私有方法呢 [
- GObject 子类对象的私有属性模拟
- GObject 子类对象的私有属性模拟
- 表达式树(公共表达式消除 uva 12219)
- Python读取文件
- Android获取屏幕宽高
- 【解决】Ubuntu无法识别U盘分区(NTFS移动硬盘无法挂载)
- SGU 476 Coach's Trouble(集合独立性、容斥、高精度)
- 对于父类的私有属性,子类是从哪里访问到的?
- Android Art Hook 技术方案
- nginx php问题集
- 折半查找(二分法)
- 使用游标更新和删除数据
- 解决Servlet中处理GET请求乱码问题
- ffmpeg AVRational结构体介绍
- Linq查询的简单使用
- java与xml之DOM创建和解析XML文档