Java 四种级别的修饰符

来源:互联网 发布:织梦pc 手机端同步插件 编辑:程序博客网 时间:2024/06/05 00:38

搬家后的博客链接: IT客栈 www.itkezhan.org


 public,protected,default,private,这四种级别的修饰符都可以用来修饰类、方法和字段。

----------------------------------------------------------------------------------------

                       类内        包内        子类        包外

  public          yes          yes         yes         yes 

 protected     yes          yes         yes         no

 default          yes          yes        no           no

 private          yes          no           no           no

----------------------------------------------------------------------------------------


还有一点就是  一个成员变量如果 private 之后就算与这个变量所在类的内部类也访问不了。


例如


public class test
{
private int a = 30;

class test2
//内部类
{
}

public test()
{
test2 s = new test2();
System.out.println(s.a);
//这句将导致程序编译不通过
}

public static void main(String[] args)
{
new test();
}
}