关于java访问权限

关于java的访问权限,我做了一张表格,见下面:



测试代码如下:

这个是基本类,其输出对应上面表格“本类“那行。

/*
 * 这个是基本类,包为basic.
 * 在这个类里定义了四个不同访问权限的int型变量,同时定义了四个不同访问权限的方法。
 * 在这个类里,可以看到通过this
 * 20170109
 */
package basic;

public class SuperClass {
	
	private int f1;					//private 变量
	int f2;							//包访问权限变量。
	protected int f3;				//protected 访问权限变量。
	public int f4;					//public 访问权限变量。
	
	public SuperClass() {
		this.f1 = 1;
		this.f2 = 2;
		this.f3 = 3;
		this.f4 = 4;
		
	}
	public SuperClass(int f1, int f2, int f3,int f4) {
		this.f1 = f1;
		this.f2 = f2;
		this.f3 = f3;
		this.f4 = f4;
	}
	
	private int accessF1() {		//private 访问权限方法。
		return f1;
	}
	
	int accessF2() {				//(无修饰)包访问权限方法。
		return f2;
	}
	
	protected int accessF3() { 		//protected访问权限方法。
		return f3;
	}
	
	public int accessF4() {			//public 访问权限方法。
		return f4;
	}
	
	/**
	 * 测试通过this 访问变量和方法。this可以不写,这里主要是为了和其他本类对象(如下面的方法里的other)区别,
	 * 所以写上了。
	 */
	
	public void outPutByThis() {
		System.out.println(this.f1);//在本类中可以直接访问private 变量。
		System.out.println(this.accessF1());	//在本类中可以直接访问private 方法。
		System.out.println(this.f2);//在本类中可以直接访问无修饰变量。
		System.out.println(this.accessF2());	//在本类中可以直接访问无修饰方法。
		System.out.println(this.f3);//在本类中可以直接访问protected 变量。
		System.out.println(this.accessF3());	//在本类中可以直接访问protected 方法。
		System.out.println(this.f4);//在本类中可以直接访问public 变量。
		System.out.println(this.accessF4());	//在本类中可以直接访问public 方法。
	}
	
	/**
	 * 测试通过同类其他对象访问四种访问权限变量和方法的能力。
	 * @param other
	 */
	public void outPutBySuperClassObject(SuperClass other) {
		System.out.println(other.f1);	//在本类中可以访问其他本类对象(不是this)的private 变量。
		System.out.println(other.accessF1());	// 在本类中可以访问其他本类对象(不是this)的private 方法。
		System.out.println(other.f2);	//在本类中可以访问其他本类对象(不是this)的无修饰 变量。
		System.out.println(other.accessF2());	// 在本类中可以访问其他本类对象(不是this)的无修饰方法。
		System.out.println(other.f3);	//在本类中可以访问其他本类对象(不是this)的protected 变量。
		System.out.println(other.accessF3());	// 在本类中可以访问其他本类对象(不是this)的protected 方法。
		System.out.println(other.f4);	//在本类中可以访问其他本类对象(不是this)的public 变量。
		System.out.println(other.accessF4());	// 在本类中可以访问其他本类对象(不是this)的public 方法。	
	}
	
	/**
	 * 程序入口。
	 * @param args
	 */
	public static void main(String[] args) {
		SuperClass s1 = new SuperClass(1,11,111,1111);
		s1.outPutByThis();
		SuperClass s2 = new SuperClass(2,22,222,2222);
		s1.outPutBySuperClassObject(s2);
	}
}

下面两个类都是SuperClass类的子类,OtherSonClassInSamePackage是与SuperClass在同一个包,OtherSonClassInDifferentPackage与SuperClass不在同一个包,用于后面的代码测试。

package basic;

public class OtherSonClassInSamePackage extends SuperClass {
	public OtherSonClassInSamePackage() {
		super();
	}
	public OtherSonClassInSamePackage(int f1, int f2, int f3, int f4) {
		super(f1,f2,f3,f4);
	}
}
package test;

import basic.SuperClass;

public class OtherSonClassInDifferentPackage extends SuperClass {
	public OtherSonClassInDifferentPackage() {
		super();
	}
	public OtherSonClassInDifferentPackage(int f1, int f2, int f3,int f4) {
		super(f1,f2,f3,f4);
	}
	
}

下面是同包子类的测试代码,对应上面表格“同包子类”输出。

/*
 * 测试调用环境为同包中的子类,多种对象对不同修饰符的变量和方法的访问权限。
 * 引入test.OtherSonClassInDifferentPackage是为了测试在同包子类中非同包子类对象对
 * 4种访问权限变量和方法的访问能力。
 * 20170109
 */
package basic;

import test.OtherSonClassInDifferentPackage;

public class SonClassInSamePackage extends SuperClass {
	public SonClassInSamePackage() {
		super();
	}
	public SonClassInSamePackage(int f1, int f2,int f3,int f4) {
		super(f1,f2,f3,f4);
	}
	/**
	 * 测试类的直接访问能力。(可以省去this).
	 */
	public void outPutByThis() {
		//System.out.println(this.f1);//不能在同包子类中直接访问父类private 变量,提示
										//The field SuperClass.f1 is not visible
		//System.out.println(this.accessF1());//不能在同包子类中直接访问父类private 方法,提示
										//The method accessF1() from the type SuperClass is not visible
		System.out.println(this.f2);//可以在同包子类中直接访问父类无修饰变量。
		System.out.println(this.accessF2());	//可以在同包子类中直接访问父类无修饰方法。
		System.out.println(this.f3);//可以在同包子类中直接访问父类protected 变量。
		System.out.println(this.accessF3());	//可以在同包子类中直接访问父类protected 方法。
		System.out.println(this.f4);//可以在同包子类中直接访问父类public 变量。
		System.out.println(this.accessF4());	//可以在同包子类中直接访问父类public 方法。
	
	}
	
	/**
	 * 测试在类里访问同类(SonClassInSamePackage)其他对象对四种变量和方法的访问能力。
	 * @param s
	 */
	public void outPutByAnotherSameClassObject(SonClassInSamePackage s) {
		//System.out.println(s.f1);	//不能在同包子类中 通过其他同类对象(不是this)直接访问父类的private 变量,提示
									//The field SuperClass.f1 is not visible
		//System.out.println(s.accessF1());	//不能在同包子类中 通过其他同类对象(不是this)直接访问父类的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		System.out.println(s.f2);	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的无修饰 变量。
		System.out.println(s.accessF2());	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的无修饰方法。
		System.out.println(s.f3);	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的protected 变量。
		System.out.println(s.accessF3());	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的protected 方法。
		System.out.println(s.f4);	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的public 变量。
		System.out.println(s.accessF4());	//可以在同包子类中 通过其他同类对象(不是this)直接访问父类的public 方法。	
	}
	
	/**
	 * 测试在同包子类中通过其他同包内子类(OtherSonClassInSamePackage)对象对4种变量和方法的访问能力。
	 * @param os
	 */
	public void outPutByOtherSonClassInSampPackage(OtherSonClassInSamePackage os) {
		//System.out.println(os.f1);//不能在同包子类中 通过同包其他子类访问父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(os.accessF1());	//不能在同包子类中 通过同包其他子类访问父类 的private 方法,提示
								//The method accessF1() from the type SuperClass is not visible
		System.out.println(os.f2);	//可以在同包子类中 通过同包其他子类访问父类 的无修饰 变量。
		System.out.println(os.accessF2());	//可以在同包子类中 通过同包其他子类访问父类 的无修饰方法。
		System.out.println(os.f3);	//可以在同包子类中 通过同包其他子类访问父类 的protected 变量。
		System.out.println(os.accessF3());	//可以在同包子类中 通过同包其他子类访问父类 的protected 方法。
		System.out.println(os.f4);	//可以在同包子类中 通过同包其他子类访问父类 的public 变量。
		System.out.println(os.accessF4());	//可以在同包子类中 通过同包其他子类访问父类 的public 方法。	

	
	}
	/**
	 * 测试在同包子类中 通过其他非同包内子类(OtherSonClassInDifferentPackage)对象对4种变量和方法的访问能力。
	 * @param osd
	 */
	public void outPutByOtherSonClassInDifferentPackage(OtherSonClassInDifferentPackage osd) {
		//System.out.println(osd.f1);//不能在同包子类中 通过非同包其他子类对象访问父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(osd.accessF1());	//不能在同包子类中 通过非同包其他子类对象访问父类 的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(osd.f2);	//不能在同包子类中 通过非同包其他子类对象访问父类 的无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(osd.accessF2());	//不能在同包子类中 通过非同包其他子类对象访问父类 的无修饰方法。
									//The method accessF2() from the type SuperClass is not visible
		System.out.println(osd.f3);	//可以在同包子类中 通过非同包其他子类对象访问父类 的protected 变量。
		System.out.println(osd.accessF3());	//可以在同包子类中 通过非同包其他子类对象访问父类 的protected 方法。
		System.out.println(osd.f4);	//可以在同包子类中 通过非同包其他子类对象访问父类 的public 变量。
		System.out.println(osd.accessF4());	//可以在同包子类中 通过非同包其他子类对象访问父类 的public 方法。	

	}
	
	/**
	 * 程序入口。
	 * @param args
	 */
	public static void main(String[] args) {
		SonClassInSamePackage s1 = new SonClassInSamePackage(1,11,111,1111);
		s1.outPutByThis();
		SonClassInSamePackage s2 = new SonClassInSamePackage(2,22,222,2222);
		s1.outPutByAnotherSameClassObject(s2);
		OtherSonClassInSamePackage os = new OtherSonClassInSamePackage(3,33,333,3333);
		s1.outPutByOtherSonClassInSampPackage(os);
		OtherSonClassInDifferentPackage osd = new OtherSonClassInDifferentPackage(4,44,444,4444);
		s1.outPutByOtherSonClassInDifferentPackage(osd);
	}
}


下面的代码是同包非子类测试代码,既然不是子类,注意this 在这里肯定是不行的,对应上面表格“同包非子类”输出。

/*
 * 测试同包中的非子类,不同对象对不同修饰符的变量和方法访问能力。
 * 这种情况,this不能用了。
 * 20170109
 */
package basic;


import test.OtherSonClassInDifferentPackage;


public class NonSonClassInSamePackage {
	public SuperClass s1 = new SuperClass(1,11,111,1111);
	public SuperClass s2 = new SuperClass(2,22,222,2222);
	
	/*
	 * 非本类,非子类不能使用this了。
	 */
	/*
	public void outPutByThis() {
		System.out.println(this.f1);//不能在同包非子类 中通过this访问SuperClass的 private 变量。
		System.out.println(this.accessF1());//不能在同包非子类中 通过this访问SuperClass的 private 方法。
		System.out.println(this.f2);//不能在同包非子类中通过this访问SuperClass的无修饰变量。
		System.out.println(this.accessF2());//不能在同包非子类中通过this访问SuperClass的无修饰方法。
		System.out.println(this.f3);//在同包非子类中通过this访问SuperClass的protected变量。
		System.out.println(this.accessF3());//不能在同包非子类中通过this访问SuperClass的protected方法。
		System.out.println(this.f4);//不能在同包非子类中通过this访问SuperClass的public变量。
		System.out.println(this.accessF4());//不能在同包非子类中通过this访问SuperClass的public方法。
	}
	*/
	/**
	 * 这个方法本是测试对同类其他对象(非this)的访问能力,但是因为现在这个类(NonSonClassInSamePackage)与SuperClass
	 * 没有关系,所以无意义。所以,这里是测试SuperClass对象在与SuperClass所在类同包但非其子类里对4种变量和方法的访问权限。
	 * @param s
	 */
	public void outPutBySuperClassObject(SuperClass s) {
		//System.out.println(s.f1);	//不能在同包非子类中 通过SuperClass对象访问访问其private 变量。
		//System.out.println(s.accessF1());	//不能在同包非子类中 通过SuperClass对象访问访问其private 方法。
		System.out.println(s.f2);	//可以在同包非子类 中通过SuperClass对象访问其无修饰 变量。
		System.out.println(s.accessF2());	//可以在同包非子类中 通过SuperClass对象访问其无修饰方法。
		System.out.println(s.f3);	//可以在同包非子类中 通过SuperClass对象访问其protected 变量。
		System.out.println(s.accessF3());	//可以在同包非子类中 通过SuperClass对象访问其protected 方法。
		System.out.println(s.f4);	//可以在同包非子类中 通过SuperClass对象访问其public 变量。
		System.out.println(s.accessF4());	//可以在同包非子类中 通过SuperClass对象访问其public 方法。	
	}
	
	/**
	 * 测试在同包非子类中,其他同包子类(OtherSonClassInSamePackage)对象对四种变量的访问能力。
	 * @param os
	 */
	public void outPutByOtherSonClassInSampPackage(OtherSonClassInSamePackage os) {
		//System.out.println(os.f1);//不能在同包非子类中 通过同包其他子类对象访问其父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(os.accessF1());	//不能在同包非子类中 通过同包其他子类对象访问其父类 的private 方法,提示
								//The method accessF1() from the type SuperClass is not visible
		System.out.println(os.f2);	//可以在同包非子类中 通过同包其他子类对象访问其父类 的无修饰 变量。
		System.out.println(os.accessF2());	//可以在同包非子类中 通过同包其他子类对象访问其父类 的无修饰方法。
		System.out.println(os.f3);	//可以在同包非子类中 通过同包其他子类对象访问其父类 的protected 变量。
		System.out.println(os.accessF3());	//可以在同包子类中 通过同包其他子类对象访问其父类 的protected 方法。
		System.out.println(os.f4);	//可以在同包子类中 通过同包其他子类对象访问其父类 的public 变量。
		System.out.println(os.accessF4());	//可以在同包子类中 通过同包其他子类对象访问其父类 的public 方法。		
	}
	
	/**
	 * 测试在同包非子类中 通过其他非同包内子类(OtherSonClassInDifferentPackage)对象对4种变量和方法的访问能力。
	 * @param osd
	 */
	public void outPutByOtherSonClassInDifferentPackage(OtherSonClassInDifferentPackage osd) {
		//System.out.println(osd.f1);//不能在同包非子类中 通过其他非同包子类对象访问其父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(osd.accessF1());	//不能在同包非子类中 通过其他非同包子类对象访问其父类 的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(osd.f2);	//不能在同包子类中 通过其他非同包子类对象访问其父类 的无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(osd.accessF2());	//不能在同包非子类中 通过其他非同包子类对象访问其父类 的无修饰方法。
									//The method accessF2() from the type SuperClass is not visible
		System.out.println(osd.f3);	//可以在同包非子类中 通过其他非同包子类对象访问其父类 的protected 变量。
		System.out.println(osd.accessF3());	//可以在同包非子类中 通过其他非同包子类对象访问其父类 的protected 方法。
		System.out.println(osd.f4);	//可以在同包非子类中 通过其他非同包子类对象访问其父类 的public 变量。
		System.out.println(osd.accessF4());	//可以在同包非子类中 通过其他非同包子类对象访问其父类 的public 方法。	


	}
	public static void main(String[] args) {
		NonSonClassInSamePackage nsc = new NonSonClassInSamePackage();
		nsc.outPutBySuperClassObject(nsc.s2);
		OtherSonClassInSamePackage os = new OtherSonClassInSamePackage(3,33,333,3333);
		nsc.outPutByOtherSonClassInSampPackage(os);
		OtherSonClassInDifferentPackage osd = new OtherSonClassInDifferentPackage(4,44,444,4444);
		nsc.outPutByOtherSonClassInDifferentPackage(osd);
	}
}


下面的代码测试非同包的子类,对应上面表格的“非同包子类” 输出。


/*
 * 测试调用环境为非同包中的子类,不同修饰符的访问权限。
 * 可以用this.
 * 20170109
 */
package test;


import basic.OtherSonClassInSamePackage;
import basic.SuperClass;


public class SonClassInDifferentPackage extends SuperClass {
	public SonClassInDifferentPackage(){
		super();
	}
	public SonClassInDifferentPackage(int f1, int f2, int f3,int f4) {
		super(f1,f2,f3,f4);
	}
	
	public void outPutByThis() {
		//System.out.println(this.f1);//不能在非同包子类中 直接访问父类private 变量,提示
									//The field SuperClass.f1 is not visible
		//System.out.println(this.accessF1());//不能在非同包子类中 直接访问父类private 方法,提示
										//The method accessF1() from the type SuperClass is not visible
		//System.out.println(this.f2);//不能在非包子类中直接访问父类无修饰变量,提示
										//The field SuperClass.f2 is not visible
		//System.out.println(this.accessF2());	//不能在非同包子类中直接访问父类无修饰方法。
									//The method accessF2() from the type SuperClass is not visible
		System.out.println(this.f3);//可以在非同包子类中直接访问父类protected 变量。
		System.out.println(this.accessF3());	//可以在非同包子类中直接访问父类protected 方法。
		System.out.println(this.f4);//可以在非同包子类中直接访问父类public 变量。
		System.out.println(this.accessF4());	//可以在非同包子类中直接访问父类public 方法。
	
	}
	
	/**
	 * 测试在非同包子类里同类(SonClassInDifferentPackage)其他对象对四种变量和方法的访问能力。
	 * @param s
	 */
	public void outPutByAnotherSameClassObject(SonClassInDifferentPackage s) {
		//System.out.println(s.f1);	//不能在非同包子类中 通过同类其他对象(不是this)直接访问其父类的private 变量,提示
									//The field SuperClass.f1 is not visible
		//System.out.println(s.accessF1());	//不能在非同包子类中 通过同类其他对象(不是this)直接访问其父类的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(s.f2);	//不能在非同包子类中 通过同类其他对象(不是this)直接访问其父类的无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(s.accessF2());	//不能在非同包子类中 通过同类其他对象(不是this)直接访问其父类的无修饰方法,提示
									//The method accessF2() from the type SuperClass is not visible
		System.out.println(s.f3);	//可以在非同包子类中 通过同类其他对象(不是this)直接访问其父类的protected 变量。
		System.out.println(s.accessF3());	//可以在非同包子类中 通过同类其他对象(不是this)直接访问其父类的protected 方法。
		System.out.println(s.f4);	//可以在非同包子类中 通过同类其他对象(不是this)直接访问其父类的public 变量。
		System.out.println(s.accessF4());	//可以在非同包子类中 通过同类其他对象(不是this)直接访问其父类的public 方法。	
	}
	
	/**
	 * 测试在非同包子类里 其他同包子类(OtherSonClassInSamePackage)对象对四种变量和方法的访问能力。
	 * @param os
	 */
	public void outPutByOtherSonClassInSampPackage(OtherSonClassInSamePackage os) {
		//System.out.println(os.f1);//不能在非同包子类中 通过同包其他子类对象访问其父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(os.accessF1());	//不能在非同包子类中 通过同包其他子类对象访问其父类 的private 方法,提示
								//The method accessF1() from the type SuperClass is not visible
		//System.out.println(os.f2);	//不能在非同包子类中 通过同包其他子类对象访问其父类 的无修饰 变量。
								The field SuperClass.f2 is not visible
		//System.out.println(os.accessF2());	//不能在非同包子类中 通过同包其他子类对象访问其父类 的无修饰方法。
								//The method accessF2() from the type SuperClass is not visible
		//System.out.println(os.f3);	//不能在非同包子类中 通过同包其他子类对象访问其父类 的protected 变量。
								//The field SuperClass.f3 is not visible
		//System.out.println(os.accessF3());	//不能在非同包子类中 通过同包其他子类对象访问其父类 的protected 方法。
								//The method accessF3() from the type SuperClass is not visible
		System.out.println(os.f4);	//可以在非同包子类中 通过同包其他子类对象访问其父类 的public 变量。
		System.out.println(os.accessF4());	//可以在非同包子类中 通过同包其他子类对象访问其父类 的public 方法。	
	}
	
	/**
	 * 测试在非同包子类里 其他 非 同包子类(OtherSonClassInDifferentPackage)对象对四种变量和方法的访问能力。
	 * @param osd
	 */
	public void outPutByOtherSonClassInDifferentPackage(OtherSonClassInDifferentPackage osd) {
		//System.out.println(osd.f1);//不能在非同包子类中 通过其他非同包子类对象访问其父类的 private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(osd.accessF1());	//不能在非同包子类中 通过其他非同包子类对象访问其父类的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(osd.f2);	//不能在非同包子类中 通过其他非同包子类对象访问其父类 的无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(osd.accessF2());	//不能在非同包子类中 通过其他非同包子类对象访问其父类 的无修饰方法。
									//The method accessF2() from the type SuperClass is not visible
		//System.out.println(osd.f3);	//不能在非同包子类中 通过其他非同包子类对象访问其父类 的protected 变量。
									//The field SuperClass.f3 is not visible
		//System.out.println(osd.accessF3());	//不能在非同包子类中 通过其他非同包子类对象访问其父类 的protected 方法。
									//The method accessF3() from the type SuperClass is not visible
		System.out.println(osd.f4);	//可以在非同包子类中 通过其他非同包子类对象访问其父类 的public 变量。
		System.out.println(osd.accessF4());	//可以在非同包子类中 通过其他非同包子类对象访问其父类 的public 方法。	


	}
	
	/**
	 * 程序入口
	 * @param args
	 */
	public static void main(String[] args) {
		SonClassInDifferentPackage s1 = new SonClassInDifferentPackage(1,11,111,1111);
		s1.outPutByThis();
		SonClassInDifferentPackage s2 = new SonClassInDifferentPackage(2,22,222,2222);
		s1.outPutByAnotherSameClassObject(s2);
		OtherSonClassInSamePackage os = new OtherSonClassInSamePackage(3,33,333,3333);
		s1.outPutByOtherSonClassInSampPackage(os);
		OtherSonClassInDifferentPackage osd = new OtherSonClassInDifferentPackage(4,44,444,4444);
		s1.outPutByOtherSonClassInDifferentPackage(osd);
	}
}

下面的代码测试非同包非子类,对应上面表格“非同包非子类”输出。


/*
 * 测试非同包非子类,不同修饰符的访问权限。
 * this 不可用了。
 */
package test;

import basic.OtherSonClassInSamePackage;
import basic.SuperClass;

public class NonSonClassInDifferentPackage {
	SuperClass s1 = new SuperClass(1,11,111,1111);
	SuperClass s2 = new SuperClass(2,22,222,2222);
	
	/*
	 * 非本类,非子类不能使用this了。
	 */
	/*
	public void outPutByThis() {
		System.out.println(this.f1);//不能在不同包非子类中通过this访问SuperClass的 private 变量。
		System.out.println(this.accessF1());//不能在不同包非子类中通过this访问SuperClass的 private 方法。
		System.out.println(this.f2);//不能在不同包非子类中通过this访问SuperClass的无修饰变量。
		System.out.println(this.accessF2());//不能在不同包非子类中通过this访问SuperClass的无修饰方法。
		System.out.println(this.f3);//不能在不同包非子类中通过this访问SuperClass的protected变量。
		System.out.println(this.accessF3());//不能在不同包非子类中通过this访问SuperClass的protected方法。
		System.out.println(this.f4);//不能在不同包非子类中通过this访问SuperClass的public变量。
		System.out.println(this.accessF4());//不能在不同包非子类中通过this访问SuperClass的public方法。
	}
	*/
	/**
	 * 这个方法本是测试对同类其他对象(非this)的访问能力,但是因为现在这个类(NonSonClassInDifferentPackage)与SuperClass
	 * 没有关系,所以无意义。所以,这里是测试SuperClass对象在与SuperClass所在类非同包非子类(NonSonClassInDifferentPackage
	 * 与SuperClass非同包非子类)里 对4种变量和方法的访问权限。
	 * @param s
	 */
	public void outPutBySuperClassObject(SuperClass s) {
		//System.out.println(s.f1);	//不能在非同包非子类中 通过SuperClass对象访问其private 变量,提示
									//The field SuperClass.f1 is not visible
		//System.out.println(s.accessF1());	//不能在非同包非子类中 通过SuperClass对象访问访问其private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(s.f2);	//不能在非同包非子类中通过SuperClass对象访问其无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(s.accessF2());	//不能在非同包非子类中通过SuperClass对象访问其无修饰方法,提示
									//The method accessF2() from the type SuperClass is not visible
		//System.out.println(s.f3);	//不能在非同包非子类中通过SuperClass对象访问其protected 变量,提示
									//The field SuperClass.f3 is not visible
		//System.out.println(s.accessF3());	//不能在非同包非子类中通过SuperClass对象访问其protected 方法,提示
									//The method accessF3() from the type SuperClass is not visible
		System.out.println(s.f4);	//可以在非同包非子类中通过SuperClass对象访问其public 变量。
		System.out.println(s.accessF4());	//可以在非同包非子类中通过SuperClass对象访问其public 方法。	
	}
	
	/**
	 * 测试在非同包非子类中,其他同包子类(OtherSonClassInSamePackage)对象对四种变量的访问能力。
	 */
	public void outPutByOtherSonClassInSampPackage(OtherSonClassInSamePackage os) {
		//System.out.println(os.f1);//不能在非同包非子类中 通过其他同包子类对象访问其父类的 private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(os.accessF1());	//不能在非同包非子类中 通过其他同包子类对象访问其父类 的private 方法,提示
								//The method accessF1() from the type SuperClass is not visible
		//System.out.println(os.f2);	//不能在非同包非子类中 通过其他同包子类对象访问其父类 的无修饰 变量。
								The field SuperClass.f2 is not visible
		//System.out.println(os.accessF2());	//不能在非同包非子类中 通过其他同包子类对象访问其父类 的无修饰方法。
								//The method accessF2() from the type SuperClass is not visible
		//System.out.println(os.f3);	//不能在非同包非子类中 通过其他同包子类对象访问其父类 的protected 变量。
								//The field SuperClass.f3 is not visible
		//System.out.println(os.accessF3());	//不能在非同包非子类中 通过其他同包子类对象访问其父类 的protected 方法。
								//The method accessF3() from the type SuperClass is not visible
		System.out.println(os.f4);	//可以在非同包非子类中 通过其他同包子类对象访问其父类 的public 变量。
		System.out.println(os.accessF4());	//可以在非同包非子类中 通过其他同包子类对象访问其父类 的public 方法。	
	}
	
	/**
	 * 测试在非同包非子类中 通过其他非同包内子类(OtherSonClassInDifferentPackage)对象对4种变量和方法的访问能力。
	 * @param osd
	 */
	public void outPutByOtherSonClassInDifferentPackage(OtherSonClassInDifferentPackage osd) {
		//System.out.println(osd.f1);//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的private 变量。提示
									//The field SuperClass.f1 is not visible
		//System.out.println(osd.accessF1());	//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的private 方法,提示
									//The method accessF1() from the type SuperClass is not visible
		//System.out.println(osd.f2);	//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的无修饰 变量,提示
									//The field SuperClass.f2 is not visible
		//System.out.println(osd.accessF2());	//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的无修饰方法。
									//The method accessF2() from the type SuperClass is not visible
		//System.out.println(osd.f3);	//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的protected 变量。
									//The field SuperClass.f3 is not visible
		//System.out.println(osd.accessF3());	//不能在非同包非子类中 通过其他非同包子类对象访问其父类 的protected 方法。
									//The method accessF3() from the type SuperClass is not visible
		System.out.println(osd.f4);	//可以在非同包非子类中 通过其他非同包子类对象访问其父类 的public 变量。
		System.out.println(osd.accessF4());	//可以在非同包非子类中 通过其他非同包子类对象访问其父类 的public 方法。	

	}
	
	/**
	 * 程序入口
	 * @param args
	 */
	public static void main(String[] args) {
		NonSonClassInDifferentPackage nsc = new NonSonClassInDifferentPackage();
		nsc.outPutBySuperClassObject(nsc.s2);
		OtherSonClassInSamePackage os = new OtherSonClassInSamePackage(3,33,333,3333);
		nsc.outPutByOtherSonClassInSampPackage(os);
		OtherSonClassInDifferentPackage osd = new OtherSonClassInDifferentPackage(4,44,444,4444);
		nsc.outPutByOtherSonClassInDifferentPackage(osd);
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值