群里讨论了很激烈的一个问题,java中类成员的可见性问题,最终我也因为一些不确定因素,没能很自信地将java中类成员的可见性说得很清楚,于是决定回过头来重新温习一下JAVA中类成员的可见性问题,下面做一下实际的测试代码。
一、类的成员可见性
二、测试代码
我在这里主要写了这么几个类文件:Father.java,Son.java,Neighbor.java,Son类继承于Father类,包的结构如下
测试1:同一类中的测试
Father.java
package test.pack.visible;
public class Father {
String sex;
private String secret;
protected int age;
public String name;
public Father() {
this.name = "ryn.xiao";
this.age = 37;
this.secret = "I love your mother when I met her";
sex = "男";
}
private void secret() {
System.out.println("I have a private secret!\n");
}
public void tellSecret() {
System.out.println("my secret is " + this.secret + "\n");
}
protected void willTell() {
System.out.println("if you study hard, I will tell you a secret of mine!\n");
}
void ignoreOthers() {
System.out.println("sorry, I don't konw you.\n");
}
public static void main(String[] args) {
Father father = new Father();
/** private test **/
System.out.println("----------private----------");
// 1.field
System.out.println("Father's secret is " + father.secret);
// 2.method
father.secret();
/** protected test **/
System.out.println("----------protected----------");
// 1.field
System.out.println("Father's age is " + father.age);
// 2.method
father.willTell();
/** public test **/
System.out.println("----------public----------");
// 1.field
System.out.println("Father's name is " + father.name);
// 2.method
father.tellSecret();
/** default test **/
System.out.println("----------default----------");
// 1.field
System.out.println("Father's sex is " + father.sex);
// 2.method
father.ignoreOthers();
}
}
打印结果:
可以看出的是所有的属性以及方法对于当前类都是可见的
Son.java
package test.pack.visible;
public class Son extends Father {
public static void main(String[] args) {
Son son = new Son();
/** private test **/
System.out.println("----------private----------");
// 1.field
System.out.println("Father's secret is " + son.secret);
// 2.method
son.secret();
/** protected test **/
System.out.println("----------protected----------");
// 1.field
System.out.println("Father's age is " + son.age);
// 2.method
son.willTell();
/** public test **/
System.out.println("----------public----------");
// 1.field
System.out.println("Father's name is " + son.name);
// 2.method
son.tellSecret();
/** default test **/
System.out.println("----------default----------");
// 1.field
System.out.println("Father's sex is " + son.sex);
// 2.method
son.ignoreOthers();
}
}
只要代码在eclipse上编辑,就会出现如下的错误提示:
注释掉错误部分:打印结果如下:
可以看到子类在同一包中访问父类中的属性和方法,除了私有的属性和方法不可以访问外,其他的都是可以访问的其实对于在同一包中的其他类来说,这个结论也是成立的,下面就是在同一包中非子类的测试代码.
测试3:同一包中非子类的测试
Neighbor.java
package test.pack.visible;
public class Neighbor {
public static void main(String[] args) {
Father father = new Father();
/** private test **/
//System.out.println("----------private----------");
// 1.field
//System.out.println("Father's secret is " + father.secret);
// 2.method
//secret();
/** protected test **/
System.out.println("----------protected----------");
// 1.field
System.out.println("Father's age is " + father.age);
// 2.method
father.willTell();
/** public test **/
System.out.println("----------public----------");
// 1.field
System.out.println("Father's name is " + father.name);
// 2.method
father.tellSecret();
/** default test **/
System.out.println("----------default----------");
// 1.field
System.out.println("Father's sex is " + father.sex);
// 2.method
father.ignoreOthers();
}
}
效果如上一样,这里就不贴图了
测试4:不同包中子类的测试代码跟上述的Son.java代码一样,只是包换了
会发现在访问私有属性以及默认属性时出现了错误,证明了不同包的子类只能访问父类的公有和保护属性以及方法
测试5:不同包中非子类的测试
代码依然跟上面的Neighbor.java一样,包换了
根据图中的编辑时错误显示,不同包中的非子类访问一个类中的属性或者方法时,只能访问其公有属性
注:可以通过在一个类中声明getter以及setter方法来提供外部类访问属性的方法