黑馬程式員——物件導向--內部類,黑馬物件導向
------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------
內部類:
內部類的訪問規則:
1.內部類可以直接存取外部類中的成員,包括私人
之所以可以直接存取外部類中的成員,是因為內部類中持有了一個外部類的引用,格式: 外部類名.this
2.外部類要訪問內部類,必須建立內部類對象。
class outer{int num = 5;class inner{System.out.println("num="+num);//可以直接調用外部成員}void method(){inner in = new inner();//建立內部類的對象show();//調用內部類的方法}}訪問格式:
1.當內部類定義在外部類的成員位置上,而且非私人,可以在外部其他類中
可以直接建立內部類對象
格式:
外部類名.內部類名 變數名 = 外部類對象.內部類對象;
outer.Inner in = new outer().new Inner();
2.當內部類在成員位置上,就可以被成員修飾符所修飾
比如:private:將內部類在外部類中進行封裝
static:內部類就具備static的特性
當內部類被static修飾後,只能直接存取外部類中的static成員,出現了訪問局限
在外部其他類中,如何直接存取static內部類的非靜態成員呢?
new outer.Inner().function();
在外部其他類中,如何直接存取static內部類的靜態成員呢?
outer.Inner.function();
注意:當內部類中定義了靜態成員,該內部類必須是static的
當外部類中的靜態方法訪問內部類時,內部類也必須是static的
當描述事物時,事物的內部還有事物,該事物類用內部類來描述。
因為內部事務在使用外部事物的內容
class Body{private class zhangcheng//內部類{}public void show(){new zhangcheng();//必須建立對象}}class Outer{private static int x = 3;static class Inner{static void function(){System.out.println("inner:"+x);}}static class Inner2{void show(){System.out.println("inner2 show");}}public static void method(){new Inner2().show();}}class InnerClassDemo2{public static void main(String args[]){Outer.Inner.function();new Outer.inner().function();//直接存取內部類中的成員Outer.Inner in = new Outer().new Inner();//建立內部類對象in.function();//調用內部類的方法}}內部類在定義時:
1.不可以被成員修飾符修飾
2.可以直接存取外部類中的成員,因為還持有外部類中的引用
但是不可以訪問他所在的局部中的變數,只能訪問被final修飾的局部變數
class Outer{int x = 3;void method(final int a){final int y = 4;class Inner{void function(){System.out.println(y);}}new Inner().function();}class InnerClassDemo{public static void main(String args[]){Outer out = new Outer();out.method(7);out.method(8);}}匿名內部類:
1.匿名內部類其實就是內部類的簡寫格式
2.匿名內部類的前提:
內部類必須是繼承一個類或者是實現介面
3.匿名內部類的格式:
new 父類或者介面(){定義子類的內容}
4.其實匿名內部類就是一個匿名子類對象,而且這個對象有點胖,可以理解為帶內容的對象
5.匿名內部類中定義的方法最好不要超過3個。
abstract class AbsDemo{abstract void show();}class outer{int x = 3;class Inner extends AbsDemo{int num = 90;void show(){System.out.println('show:"+num);}void abc(){System.out.println("abc");}}public void function(){AbsDemo d = new AbsDemo(){int num = 9;void show(){System.out.println("num="+num);}void abc(){System.out.println("abcccc");}};//注意這個分號d.show();//ok,實現了介面d.abc();//error,自訂方法,沒有實現介面或者是繼承一個類,抽象類別不能繼承,只能實現}}class InnerclassDemo{public static void main(String args[]){new Outer().function();}}interface Inter{void method();}class test{static class Inner implements Inter{public void method(){System.out.println("method run");}}static Inter function(){return new Inter(){public void method(){System.out.println("method run");}};}}class Innerclasstest{public static void main(String args[]){//test.function():test類中有一個靜態方法function//method():function這個方法運算後的解惑是一個對象,而且是一個Inner類型的對象//因為只有是Inter類型的對象,才可以調用method方法test.function().method();inter in = test.function();in.method();show(new Inter(){public void method(){System.out.println("method show run");}});}public static void show(Inter in){in.method();}}class Innertest{public static void main(String args[]){new object(){public static function(){}}.function();}}