JAVA类成员的可见性

群里讨论了很激烈的一个问题,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();
	}
}

打印结果:

可以看出的是所有的属性以及方法对于当前类都是可见的


测试2:同一包中子类的测试

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方法来提供外部类访问属性的方法




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值