package 2; static ***************谢!比如创建对象,最好能稍做讲解。
package p1;public class Original
{
int n_friendly = 1;
private int n_private = 2;
protected int n_protected = 3;
public int n_public = 4;
void Access()
{
System.out.println("**** In same class,you can access...");
System.out.println("friendly member" +n_friendly);
System.out.println("private member" +n_private);
System.out.println("protected member" +n_protected);
System.out.println("public member" +n_public);
}
class Derived extends Original
{
void Access()
{
System.out.println("**** 相同包的子类 ****");
System.out.println("friendly member" +n_friendly);
//System.out.println("private member" +n_private); 不能访问
System.out.println("protected member" +n_protected);
System.out.println("public member" +n_public);
Original o = new Original();
System.out.println("**** 相同包的子类的其他对象****");
System.out.println("friendly member" +o.n_friendly);
//System.out.println("private member" +o.n_private); 不能访问
System.out.println("protected member" +o.n_protected);
System.out.println("public member" +o.n_public);
}
}
class SamePackageClass
{
void Access()
{
Original o = new Original(); //?
System.out.println("**** 相同包的其他类 ****");
System.out.println("friendly member" +o.n_friendly);
//System.out.println("private member" +o.n_private); 不能访问
System.out.println("protected member" +o.n_protected);
System.out.println("public member" +o.n_public);
}
}
class AccessControl
{
public static void main(String[] args)
{
Original o = new Original();
o.Access();
Derived d = new Derived();
d.Access();
SamePackageClass s = new SamePackageClass();
s.Access();
}
}
package p2;
class Derived extends p1.Original
{
void Access()
{
System.out.println("**** 不同包中的子类 ****");
//System.out.println("friendly member" +n_friendly);
//不能访问
//System.out.println("private member" +n_private);
System.out.println("protected member" +n_protected);
//子类可以访问父类
System.out.println("public member" +n_public);
p1.Original O = new Original();
System.out.println("**** 访问在不同包中的父类 ****");
//System.out.println("friendly member" +o.n_friendly);
//System.out.println("private member" +o.n_private); 不能访问
//System.out.println("protected member" +o.n_protected);
System.out.println("public member" +o.n_public);
}
}
class AnotherPackageClass
{
void Access()
{
p1.Original o = new p1.Original();
System.out.println("**** 另一包中的其他类 ****");
//System.out.println("friendly member" +o.n_friendly);
//System.out.println("private member" +o.n_private); 不能访问
//System.out.println("protected member" +o.n_protected);
System.out.println("public member" +o.n_public);
}
}
public class AccessControl
{
public static void main (String [] args)
{
Derived d = new Derived();
d.Access();
AnotherPackageClass a =new AnotherPackageClass();
a.Access();
}
}
}
----------------解决方案--------------------------------------------------------