public class TestInnerClass {
/**It is a test of inner class.
* @param args
*/
public static void main(String[] args) {
Animal a = new Animal();
Animal.Dog b = a.new Dog();
//在外圍類Animal的作用域之外的引用,outclass.innerclass classname = outclassName.new innerclass()
b.look();
a.output(200);
}
}
class Animal {
public class Dog {
public void look() {
System.out.println("The dog \"hong!hong!\"");
}
}
public void output(final int x) {//局部內部類可以訪問局部變量,不過要聲明爲final,否則會出現編譯錯誤
//局部內部類,沒有public之類的修飾符,只能在方法內部使用
class Cat {
public void out() {
System.out.println("The cat \"miao!miao!\"" + x);
}
}
Cat c = new Cat();
c.out();
}
}
內部類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.