package fs11;
public class jc {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int a=1;
int b=2;
protected int c=3;
private int d=4;
}
class jc1 extends jc {
void method() {
jc1 a1= new jc1();
a1.a=1;
a1.b=2;
a1.c=3;
a1.d=4; //The field jc.d is not visible
a=1;
b=2;
c=3;
d=4; //The field jc.d is not visible
}
}
在第一个包中定义一个public型的类jc,同时在里面定义4个分别为默认,public,protected,private型的成员变量,在同一个包中创建另一个类jc1,且继承类jc,程序无法运行,可以得出结论,在同一个包中同一个.java源文件中,一个类继承另一个类时,会继承里面的默认,public,protected三个类型的成员对象。
package fs11;
public class jc2 extends jc {
void method() {
jc2 a1 = new jc2();
a1.a=1;
a1.b=2;
a1.c=3;
a1.d=4; //The field jc.d is not visible
a=1;
b=2;
c=3;
d=4; //The field jc.d is not visible
}
}
同时在同一个包不同.java源文件,也是如此。
package fs22;
import fs11.jc;
class fs21 extends jc{
void method() {
fs21 a1= new fs21();
a1.a=1;
a1.b=2; //The field jc.b is not visible
a1.c=3;
a1.d=4;
a=1;
b=2;
c=3;
d=4; //The field jc.b is not visible
}
}
在另一个包中,引入该public类,创建另一个继承自该类的类,可以看出,除了protected和public类型的成员变量,其他都无法得到。
另一方面,如果把第一个公共类改为默认型的,另一个包中即使导入了第一个类,但仍旧无法得到这个类中的任何成员变量,即
package fs22;
import fs11.jc; //The type fs11.jc is not visible
class fs21 extends jc{ //jc cannot be resolved to a type
void method() {
fs21 a1= new fs21();
a1.a=1; //a cannot be resolved or is not a field
a1.b=2; //b cannot be resolved or is not a field
a1.c=3; //c cannot be resolved or is not a field
a1.d=4; //d cannot be resolved or is not a field
a=1; //a cannot be resolved or is not a field
b=2; //b cannot be resolved or is not a field
c=3; //c cannot be resolved or is not a field
d=4; //d cannot be resolved or is not a field
}
}
但同一个包中的两个.java源文件不受影响