Think in java 答案_Chapter 5_Exercise 6
来源:互联网 发布:全民淘宝客app 编辑:程序博客网 时间:2024/04/28 14:41
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
/****************** Exercise 6 ******************
* Create a class with public, private,
* protected, and "friendly" data members and
* method members. Create an object of this class
* and see what kind of compiler messages you get
* when you try to access all the class members.
* Be aware that classes in the same directory
* are part of the "default" package.
***********************************************/
package c05;
public class E06_AccessControl {
public int a;
private int b;
protected int c;
int d; // "Friendly"
public void f1() {}
private void f2() {}
protected void f3() {}
void f4() {} // "Friendly"
public static void main(String args[]) {
E06_AccessControl test =
new E06_AccessControl();
// No problem accessing everything inside
// of main() for this class, since main()
// is a member and therefore has access:
test.a = 1;
test.b = 2;
test.c = 3;
test.d = 4;
test.f1();
test.f2();
test.f3();
test.f4();
}
}
//+M java c05.E06_AccessControl
**You can see that main( ) has access to everything because it’s a member. If you create a separate class within the same package, that class cannot access the private members:
// A separate class in the same package cannot// access private elements:package c05;public class E06_Other { public E06_Other() { E06_AccessControl test = new E06_AccessControl(); test.a = 1; //! test.b = 2; // Can't access: private test.c = 3; test.d = 4; test.f1(); //! test.f2(); // Can't access: private test.f3(); test.f4(); }}
**If you create a class in a separate package (which you can do either by explicitly giving a package statement, or by simply putting it in a different directory) then it is unable to access anything but public members:
// A separate class in the same package cannot// access private elements:package c05.other2;public class E06_Other2 { public E06_Other2() { c05.E06_AccessControl test = new c05.E06_AccessControl(); test.a = 1; //! test.b = 2; // Can't access: private //! test.c = 3; // Can't access: protected //! test.d = 4; // Can't access: friendly test.f1(); //! test.f2(); // Can't access: private //! test.f3(); // Can't access: protected //! test.f4(); // Can't access: friendly }}
- Think in java 答案_Chapter 5_Exercise 6
- Think in java 答案_Chapter 5_Exercise 5
- Think in java 答案_Chapter 2_Exercise 6
- Think in java 答案_Chapter 3_Exercise 6
- Think in java 答案_Chapter 4_Exercise 6
- Think in java 答案_Chapter 2_Exercise 5
- Think in java 答案_Chapter 3_Exercise 5
- Think in java 答案_Chapter 4_Exercise 5
- Think in java 答案_Chapter 5_Exercise 1
- Think in java 答案_Chapter 5_Exercise 2
- Think in java 答案_Chapter 5_Exercise 3
- Think in java 答案_Chapter 5_Exercise 4
- Think in java 答案_Chapter 5_Exercise 7
- Think in java 答案_Chapter 5_Exercise 8
- Think in java 答案_Chapter 5_Exercise 9
- Think in java 答案_Chapter 5_Exercise 10
- Think in java 答案_Chapter 5_Exercise 11
- Think in java 答案_Chapter 5_Exercise 12
- Linux下C语言实现的简单使用线程向FIFO里写入与读取数据的例子
- 慎用@@IDENTITY
- 每页都有的表头和打印分页
- 怎么做减压又强身的睡前操?
- 页面显示按钮,打印时不显示
- Think in java 答案_Chapter 5_Exercise 6
- InstallShield------自定义对话框
- 无边框文本框
- 大数据类型通过存储过程保存数据(clob,blob)
- 巴克斯范式
- Think in java 答案_Chapter 5_Exercise 7
- Hibernate的对象关系映射(4)
- Think in java 答案_Chapter 5_Exercise 8
- 代码页"858"无效或未安装