Posted on 2006-07-06 18:13 
负人博客 阅读(333) 
评论(0)  编辑  收藏  所属分类: 
JAVA技术 
			 
			
		 
		
		对于protected关键字经常在使用中弄混,特写一小例进行测试
Protected方法或属性例子:
		
				
Package com.first;
		Public class Test1 {
 Protected String a;
Protected void test() {
  System.out.println(“it is a test”);
}
}
package com.second;
import com.first.*;
public class Test2 extends Test1 {
 Test1 test1 = new Test1();
Test2 test2 = new Test2();
test1.a = “test”;//错误
test2.a = “test”; //正确
test1.test();//错误
test2.test();//正确
}
///原因,protected定义的东西在不同包内不能访问,只有继承的子类可以访问