本类 | 同一包下的类 | 不同包下子类 | 不同包下无关类 | |
---|---|---|---|---|
private | Y | |||
默认的 | Y | Y | ||
protected | Y | Y | Y | |
public | Y | Y | Y | Y |
package a1.a2.a3; class Father{ private void show(){ System.out.println("show"); } void show2(){ System.out.println("show2"); } protected void show3(){ System.out.println("show3"); } public void show4(){ System.out.println("show4"); } public static void main(String[] args){ Father f =new Father(); f.show(); f.show2(); f.show3(); f.show4(); } }
package a1.a2.a3; class Son extends Father{ public static void main(String[] args){ Father f =new Father(); //f.show(); //编译时直接报错 f.show2(); f.show3(); f.show4(); System.out.println("------------"); Son s =new Son(); //s.show(); //编译时直接报错 s.show2(); s.show3(); s.show4(); } }
package a1.a2.a3; class Test{ public static void main(String[] args){ Father f =new Father(); //f.show(); //编译直接报错 f.show2(); f.show3(); f.show4(); } }
package b1.b2.b3;
import a1.a2.a3.Father;
class Son2 extends Father{
public static void main(String[] args){
Father f =new Father();
//f.show(); //编译时直接报错
//f.show2(); //编译时直接报错
//f.show3(); //编译时直接报错
f.show4();
System.out.println("------------");
//son2可以访问protects
Son2 s =new Son2();
//s.show(); //编译时直接报错
//s.show2(); //编译时直接报错
s.show3();
s.show4();
}
}
package b1.b2.b3; import a1.a2.a3.Father; class Test2{ public static void main(String[] args){ Father f =new Father(); //f.show(); //编译时直接报错 //f.show2(); //编译时直接报错 //f.show3(); //编译时直接报错 f.show4(); } }
权限修饰符:private.默认的.protected.public
状态修饰符:static.final
抽象修饰符:abstract
权限修饰符:默认修饰符.public
状态修饰符:final //不允许使用stitac
抽象修饰符:abstract
权限修饰符:private.默认的.protected.public
状态修饰符:static.final
public class Demo{ private int x = 20; int y = 20; protected int z = 40; public int a = 40 ; public final int b =50 ; public static int c =60; public static final int d = 70 ; }
权限修饰符:private.默认的.protected.public
public class Demo{ private Demo(){} Demo(String name){} protected Demo(String name ,int age){} public Demo(Stringname,int age ,String address){} //此处不允许使用静态,final,abstract //public static Demo(){} //public final Demo(){} //public abstract Demo(){} }
权限修饰符:private.默认的.protected.public
状态修饰符:static.final
抽象修饰符:abstract
原文地址:http://3453311.blog.51cto.com/3443311/1747288