Use Private Members from Base Class
最近看了一段代码:在导出类中调用继承自基类的某个public函数,该函数中对基类中的private数据成员
进行了赋值并将结果打印。看到程序的运行结果后,顿时感觉自己之前(我之前的理解这里就不说明了)对于
基类/导出类中私有数据成员的理解并不是很准确。所以写了下面的这个程序,进行简单的验证并加深理解:
class Insect {
private int i = 9;
Insect() {
System.out.println("Insect.");
}
private int x1 = printInit("Insect.x1 initialized");
//在导出类中不能访问基类的private成员函数print(),但可以访问printInit()(通过printInit()来访问private成员函数print()).
int printInit(String s) {
System.out.println(this.toString());
this.print(s);
return 47;
}
private void print(String s){
System.out.println(s);
}
//在导出类中不能访问基类的private数据成员i,但可以访问usePrivateVar()(通过usePrivateVar()来访问private数据成员).
public void usePrivateVar(){
System.out.println(this.toString());
System.out.println("i = " + this.i);
}
}
public class beetle extends Insect {
private int x2;
private int k = printInit("beetle.k initialized");
{
x2 = printInit("beetle.x2 initialized");
}
public beetle() {
System.out.println("beetle.");
}
public static void main(String[] args) {
beetle b = new beetle();
b.usePrivateVar();
}
}
Output:
beetle@1db9742 -- NOTE: NOT Insect.
Insect.x1 initialized
Insect.
beetle@1db9742
beetle.k initialized
beetle@1db9742
beetle.x2 initialized
beetle.
beetle@1db9742
i = 9
此外,注意beetle.x2 initialized 和 beetle.k initialized 的顺序. 与两个变量声明的顺序无关,而是
由printInit()语句的调用顺序(两个变量初始化的顺序)决定。
Use Private Members from Base Class的更多相关文章
- Private Members in JavaScript
Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...
- C++中public、protected以及private的使用
相比C语言,C++中通过class/struct来定义既包含数据,又包含行为的结构,从而支持了“对象”.现实世界中,一个人(一个对象)通常 拥有一些资产(数据),并且掌握某些技能(行为),并且这些资产 ...
- private是自己私有的,protected是可以让孩子知道的,public是公开的
三种访问权限 public:可以被任意实体访问,数据成员和函数成员可在成员函数,友元,继承类中直接使用.亦可以作为接口,供类的用户使用 protected:只允许子类及本类的成员函数访问,在基类中用法 ...
- Public Private Protect Inheritance and access specifiers
In the previous lessons on inheritance, we've been making all of our data members public in order to ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- 訪问控制 protected, public, private 对照
OOP 3大特性:数据抽象,继承,动态绑定 3中訪问标号 protected, public, private 对照 用类进行数据抽象:用继承类继承基类的成员,实现继承.通过将基类对应函数声明为vir ...
- SharePoint 2013 Apps TokenHelper SharePointContext OAuth Provider-Hosted App (抄袭,测试 csc.rsp 用)
namespace Microshaoft.SharePointApps { using Microsoft.IdentityModel; using Microsoft.IdentityModel. ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 高尔夫管理系统SSH
登录-----------http://localhost:8080/GOLF/Denglu 一.Action 1.处理今日消费数据逻辑的 package com.chinasofti.golf.ac ...
随机推荐
- [na]整一下博客面貌--cnblog css定制
前言 之前觉得cnblog排版乱. 而csdn对word兼容性较好一些.所以就转到csdn了. 后来看到别人的cnblog排版挺好,如 等效果. 参考资料 http://www.cnblogs.com ...
- 转:SQL2008 UNPIVOT 列转行示例
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int); GO INSERT INTO pv ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- C++加密解密库之选择
项目中有这样一个需求,客户端登陆服务器时,为保证信息安全,需要对用户的密码进行加密传输,在服务器端接受到之后进行相应的解密. 一.加密算法分类 对称加密算法.不对称加密算法.不可逆加密算法 1.对称加 ...
- c++ telescoping constructor is NOT supported until c++11
Telescoping constructor: see Effective Java 2nd Edition Item 2 If you want to use telescoping constr ...
- java.lang.NoClassDefFoundError: org/junit/rules/TestRule
错误原因:通过定位发现是找不到TestRule这个类,检查项目引用的Junit版本为4.7,发现TestRule是在Junit版本4.10后添加的新特性 解决方法:把junit版本由4.7改成4.10
- PLSQL快捷键设置
1.在PL/SQL Developer中编写sql语句时,如果无法自动提示字段那是一件痛苦的事情,工作效率又低,在此演示下如何在PL/SQL Developer工具中自动提示字段,让开发者省时又省心, ...
- openWRT自学---对官方的开发指导文档的解读和理解 记录3:一些常用方法
1.约定 configuration files follow the convention: <name>.conf init files follow the convention: ...
- ubuntu 12.10 apt-get 源
更改apt-get源配置文件/etc/apt/sources.list 用一下内容替换掉 deb http://mirrors.163.com/ubuntu/ precise main restric ...
- LeetCode207. Course Schedule
Description There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses m ...