接口interface和private私有內(nèi)部類怎樣一塊配合著用

接口interface和private內(nèi)部類協(xié)同工作【新手可忽略不影響繼續(xù)學(xué)習(xí)】
馬克-to-win:由于是private內(nèi)部類,外面無法訪問甚至無法看到你編的源代碼(如果在不同的包中),非常安全。外界只能調(diào)用接口中的方法。下例中訪問不了Core,甚至你不知道有Core的存在。給你的就是外部的接口,供你使用。馬克-to-win:我們一直沒講class 如何能private, 這里內(nèi)部類時,就可以用private了。且內(nèi)部類隨便訪問外部類的東西, 這就非常有力度了, 可以用到外部類所有的資源!
馬克- to-win:馬克 java社區(qū):防盜版實名手機(jī)尾號: 73203。



例2.3---本章源碼
interface CoreI
{  
    void display();
}
class ShellMark_to_win {
    int shell_x = 100;
    static int n;
    // 下面內(nèi)部類是private,只能外層類的方法才能訪問到, 非常安全
    private class Core implements CoreI {
/* 下一句錯誤,馬克-to-win:根據(jù)語法:靜態(tài)的域或方法只能出現(xiàn)在靜態(tài)類或最外層類上。The field m cannot be declared static; static fields can only be declared in static inner class or top level classes,*/     
  //    static int m=9;
        int y = 10; // y is local to core
        public void display() {
            shell_x=shell_x+20;
            n=n+1;//馬克-to-win:輕松訪問外層類的靜態(tài)變量
            System.out.println("n is "+n+" display: shell_x and y " + shell_x + " "+ShellMark_to_win.this.shell_x+ " " + y+ " "+this.y);
        }
    }
    Core newC()
    {
        return new Core();
    }
}
public class Test {
    public static void main(String args[]) {
        ShellMark_to_win shell = new ShellMark_to_win();
     //   ShellMark_to_win.Core sc=shell.new Core();//錯誤,馬克-to-win: 因為Core是私有的
        CoreI sc1=shell.newC();
        sc1.display();
    }
}

結(jié)果是:
n is 1 display: shell_x and y 120 120 10 10