什么是實例內(nèi)部類 Instance inner class有什么語法
1.Instance inner class定義,用途和用法
馬克- to-win:馬克 java社區(qū):防盜版實名手機(jī)尾號: 73203。
重要語法:馬克-to-win:1)實例內(nèi)部類一定得有個外層類的實例和它綁定在一起,所以可以用This指針。所以必須先實例化外層類之后才能再實例化內(nèi)部類。(生活中的例子就是子宮和胚胎(不算試管嬰兒?。?)語法規(guī)定:實例內(nèi)部類不能有靜態(tài)的屬性或方法,為什么?因為沒有外層類的實例就不應(yīng)該有實例內(nèi)部類的任何東西存在,包括內(nèi)部類的靜態(tài)屬性,但靜態(tài)屬性應(yīng)該在main方法執(zhí)行時創(chuàng)建,這樣就會產(chǎn)生矛盾,所以規(guī)定實例內(nèi)部類不能有靜態(tài)的屬性或方法。馬克-to-win:2)既然每個內(nèi)部類實例都可以改變他們共同的外層類的靜態(tài)屬性或?qū)嵗龑傩?,他們成為?nèi)部類實例們可以交互的地方。(下例中的 shell_x,在不斷增長。)
例2.1a:類中有個內(nèi)部類屬性。
class ShellMark_to_win {
int shell_x = 100;//既然每個內(nèi)部類實例都可以改變這里的外層類靜態(tài)屬性或?qū)嵗龑傩裕R克-to-win:這里成為內(nèi)部類實例們可以交互的地方
static int n;
Core core;
void visitCore() {
core = new Core();
core.y=8;
core.display();
}
// 下面是個實例內(nèi)部類,必須有個外層類實例,才能有這個內(nèi)部類實例。所以就有了this這個概念。
class Core {
/* 下一句錯誤,根據(jù)語法:馬克-to-win:靜態(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
void display() {
shell_x=shell_x+20;
n=n+1;//輕松訪問外層類的靜態(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();
}
void showy() {
// y=9; // 錯誤,馬克-to-win:外層類不能直接訪問內(nèi)部類的屬性。error,y not known here! System.out.println(y);
}
}
public class Test {
public static void main(String args[]) {
ShellMark_to_win shell = new ShellMark_to_win();
shell.visitCore();
ShellMark_to_win.Core sc=shell.new Core();//內(nèi)部類實例是存在于外部對象里的。
sc.display();
ShellMark_to_win.Core sc1=shell.newC();
sc1.display();
/* 不能像下面這樣寫, 因為Core 是內(nèi)部類。 can not resolve Core class ,because Core class are defiened as ShellMark_to_win's inner class. */
//Core core = new Core();
}
}
結(jié)果:
n is 1 display: shell_x and y 120 120 8 8
n is 2 display: shell_x and y 140 140 10 10
n is 3 display: shell_x and y 160 160 10 10
例2.1:---本章源碼
class ShellMark_to_win {
int shell_x = 100;//既然每個內(nèi)部類實例都可以改變這里的外層類靜態(tài)屬性或?qū)嵗龑傩裕R克-to-win:這里成為內(nèi)部類實例們可以交互的地方
static int n;
void visitCore() {
Core core = new Core();
core.y=8;
core.display();
}
// 下面是個實例內(nèi)部類,必須有個外層類實例,才能有這個內(nèi)部類實例。所以就有了this這個概念。
class Core {
/* 下一句錯誤,根據(jù)語法:馬克-to-win:靜態(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
void display() {
shell_x=shell_x+20;
n=n+1;//輕松訪問外層類的靜態(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();
}
void showy() {
// y=9; // 錯誤,馬克-to-win:外層類不能直接訪問內(nèi)部類的屬性。error,y not known here! System.out.println(y);
}
}
public class Test {
public static void main(String args[]) {
ShellMark_to_win shell = new ShellMark_to_win();
shell.visitCore();
ShellMark_to_win.Core sc=shell.new Core();//內(nèi)部類是存在于外部對象里的。
sc.display();
ShellMark_to_win.Core sc1=shell.newC();
sc1.display();
/* 不能像下面這樣寫, 因為Core 是內(nèi)部類。 can not resolve Core class ,because Core class are defiened as ShellMark_to_win's inner class. */
//Core core = new Core();
}
}
結(jié)果如下:
n is 1 display: shell_x and y 120 120 8 8
n is 2 display: shell_x and y 140 140 10 10
n is 3 display: shell_x and y 160 160 10 10