W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
內(nèi)部類可以訪問其所有實例成員,實例字段和其封閉類的實例方法。
class Outer { private int value = 2014; public class Inner { public void printValue() { System.out.println("Inner: Value = " + value); } } // Inner class ends here public void printValue() { System.out.println("Outer: Value = " + value); } public void setValue(int newValue) { this.value = newValue; } } public class Main { public static void main(String[] args) { Outer out = new Outer(); Outer.Inner in = out.new Inner(); out.printValue(); in.printValue(); out.setValue(2015); out.printValue(); in.printValue(); } }
上面的代碼生成以下結(jié)果。
以下代碼顯示如何訪問內(nèi)部類的內(nèi)部變量。
public class Main { public static void main(String[] args) { Outer out = new Outer(); Outer.Inner in = out.new Inner(); out.printValue(); in.printValue(); out.setValue(3); out.printValue(); in.printValue(); } } class Outer { private int value = 1; public class Inner { private int value = 2; public void printValue() { System.out.println("Inner: Value = " + value); } } // Inner class ends here public void printValue() { System.out.println("Outer: Value = " + value); } public void setValue(int newValue) { this.value = newValue; } }
上面的代碼生成以下結(jié)果。
以下代碼顯示如何在內(nèi)部類中使用關(guān)鍵字this。
class Outer { private int value = 1; class QualifiedThis { private int value = 2; public void printValue() { System.out.println("value=" + value); System.out.println("this.value=" + this.value); System.out.println("QualifiedThis.this.value=" + QualifiedThis.this.value); } public void printHiddenValue() { int value = 2; System.out.println("value=" + value); System.out.println("this.value=" + this.value); System.out.println("QualifiedThis.this.value=" + QualifiedThis.this.value); } } public void printValue() { System.out.println("value=" + value); System.out.println("this.value=" + this.value); } } public class Main { public static void main(String[] args) { Outer outer = new Outer(); Outer.QualifiedThis qt = outer.new QualifiedThis(); System.out.println("printValue():"); qt.printValue(); System.out.println("printHiddenValue():"); qt.printHiddenValue(); outer.printValue(); } }
上面的代碼生成以下結(jié)果。
如果實例變量名稱被隱藏,您必須使用關(guān)鍵字this或類名稱以及關(guān)鍵字this限定其名稱。
class TopLevelOuter { private int v1 = 100; // Here, only v1 is in scope public class InnerLevelOne { private int v2 = 200; // Here, only v1 and v2 are in scope public class InnerLevelTwo { private int v3 = 300; // Here, only v1, v2, and v3 are in scope public class InnerLevelThree { private int v4 = 400; // Here, all v1, v2, v3, and v4 are in scope } } } }
以下代碼顯示如何從外部類引用變量。
public class Test{ private int value = 1; public class Inner { private int value = 2; public void printValue() { System.out.println("Inner: Value = " + value); System.out.println("Outer: Value = " + Test.this.value); } } // Inner class ends here public void printValue() { System.out.println("\nOuter - printValue()..."); System.out.println("Outer: Value = " + value); } public void setValue(int newValue) { System.out.println("\nSetting Outer"s value to " + newValue); this.value = newValue; } }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: