記得在一次面試的筆試題中,有的面試官會要求寫出具體的像pullic這些訪問限定符的作用域。其實,平常我都沒去系統的考慮這些訪問限定符的作用域,特別是包內包外的情況,OK,筆試不行了。
這是java基本的知識,也是公司看重的,那沒辦法啦,我的腦袋記不住東西,那我只能把這些東西寫下來方便自己溫故知新,不廢話了,貼代碼了。
代碼如下:
package com.jaovo;/** *_1_ 成員變量訪問權限的求證 * public private protected default(默認的權限) *自己包自己類 可訪問 可訪問 可訪問 可訪問 *自己包別的類 可訪問 不可訪問 可訪問 可訪問 *別的包別的類 可訪問 不可訪問 不可訪問 不可訪問 *自己包別的類有繼承關系 可訪問 不可訪問 可訪問 可訪問 *別的包別的類有繼承關系 可訪問 不可訪問 可訪問 不可訪問 *--------------------------------------------------------------------------- *_2_ 成員方法訪問權限的求證 * public private protected default(默認的權限) *自己包自己類 可訪問 可訪問 可訪問 可訪問 *自己包別的類 可訪問 不可訪問 可訪問 可訪問 *別的包別的類 可訪問 不可訪問 不可訪問 不可訪問 *自己包別的類有繼承關系 可訪問 不可訪問 可訪問 可訪問 *別的包別的類有繼承關系 可訪問 不可訪問 可訪問 不可訪問 */import cn.jaovo.D;import cn.jaovo.E;//public class Chengyuan{//public class Chengyuan extends C{//public class Chengyuan extends D{public class Chengyuan extends E{ public static void main(String[] args){ // 一 成員變量的權限訪問 /*1 B b = new B();//自己包別的類 System.out.println( b.i1 ); System.out.println( b.i2 ); System.out.println( b.i3 );//i3可以在B中訪問private System.out.println( b.i4 ); */ /*2 Chengyuan ch = new Chengyuan();//自己包自己類 System.out.println( ch.i1 ); System.out.println( ch.i2 ); System.out.println( ch.i3 ); System.out.println( ch.i4 ); */ /*3 D d = new D();//別的包別的類 System.out.println( d.i1 ); System.out.println( d.i2 );//i2 在D中不是公共的;無法從外部程序包中對其進行訪問 System.out.println( d.i3 );//i3可以在D中訪問private System.out.println( d.i4 );//i4可以在D中訪問protected */ /*4 Chengyuan ch = new Chengyuan();//自己包別的類有繼承關系Chengyuan extends C System.out.println( ch.i1 ); System.out.println( ch.i2 ); System.out.println( ch.i3 );//i3可以在B中訪問 System.out.println( ch.i4 ); */ /*5 Chengyuan ch = new Chengyuan();//別的包別的類有繼承關系Chengyuan extends D System.out.println( ch.i1 ); System.out.println( ch.i2 );//i2在D中不是公共的;無法從外部程序包中對其進行訪問 System.out.println( ch.i3 );//i3可以在D中訪問private System.out.println( ch.i4 ); *///====================================================== //二 成員方法的訪問權限 /*1 Chengyuan ch = new Chengyuan();//自己包自己類 System.out.println( ch.m1() ); System.out.println( ch.m2() ); System.out.println( ch.m3() ); System.out.println( ch.m4() ); */ /*2 B b = new B();//自己包別的類 System.out.println( b.m1() ); System.out.println( b.m2() ); System.out.println( b.m3() );//m3()可以在B中訪問private System.out.println( b.m4() ); */ /*3 E e = new E();//別的包 別的類 System.out.println( e.m1() ); System.out.println( e.m2() );//m2在E中不是公共的;無法從外部程序包中對其進行訪問 System.out.println( e.m3() );//m3可以在E中訪問private System.out.println( e.m4() ); //m4()可以在E中訪問protected */ /*4 C c = new C();//自己包別的類有繼承關系Chengyuan extends C System.out.println( c.m1() ); System.out.println( c.m2() ); System.out.println( c.m3() );//m3()可以在C中訪問 System.out.println( c.m4() ); */ //5 Chengyuan ch = new Chengyuan(); System.out.println( ch.m1() ); System.out.println( ch.m2() );//找不到符號 System.out.println( ch.m3() );//找不到符號 System.out.println( ch.m4() ); }}class B{ //1 成員變量 public int i1 = 100; int i2 = 200; private int i3 = 300; protected int i4 = 400; //2 成員方法 public int m1(){return 1;} int m2(){return 1;} private int m3(){return 1;} protected int m4(){return 1;}}class C{ //1 成員變量 public int i1 = 100; int i2 = 200; private int i3 = 300; protected int i4 = 400; //2 成員方法 public int m1(){return 1;} int m2(){return 1;} private int m3(){return 1;} protected int m4(){return 1;}}//========================================================//D.class文件和E.class文件在cn包內,為了方便把他們放到這里package cn.jaovo;public class D{ //1 成員變量 public int i1 = 100; int i2 = 200; private int i3 = 300; protected int i4 = 400; //2 成員方法 public int m1(){return 1;} int m2(){return 1;} private int m3(){return 1;} protected int m4(){return 1;}}//-------package cn.jaovo;public class E{ //1 成員變量 public int i1 = 100; int i2 = 200; private int i3 = 300; protected int i4 = 400; //2 成員方法 public int m1(){return 1;} int m2(){return 1;} private int m3(){return 1;} protected int m4(){return 1;}}
以上代碼是Java中成員方法與成員變量訪問權限詳解,希望大家喜歡。
新聞熱點
疑難解答