1. 程式人生 > 實用技巧 >Java中的內部類

Java中的內部類

成員內部類

定義在一個類的內部。

1、內部類可以訪問外部類所有的屬性和方法,包括private成員。可以用private、protected、public修飾

 1 @Data
 2 @ToString
 3 public class Outer {
 4     private double radius=0;
 5     private static String name="Circle";
 6     public Outer(double radius){
 7         this.radius=radius;
 8     }
 9     private void outerMethod(){
10 System.out.println("outerMethod"); 11 } 12 //內部類 13 class Inner{ 14 public Inner(){ 15 16 } 17 public void innerMethod(){ 18 System.out.println("innerMethod"); 19 //訪問外部類的私有屬性 20 System.out.println("radius:"+radius); 21 //
訪問外部類的靜態屬性 22 System.out.println("name:"+name); 23 //訪問外部類的私有方法 24 outerMethod(); 25 } 26 } 27 }

內部類的建立需要一個外部類物件,可以通過如下的方法建立

1 public class MainApp {
2     public static void main(String[]args){
3         Outer outer=new Outer(10);
4         //建立成員內部類的方法
5
Outer.Inner inner=outer.new Inner(); 6 inner.innerMethod(); 7 } 8 }

執行結果

innerMethod radius:10.0 name:Circle outerMethod

2、如果外部類訪問內部類的屬性和方法需要先建立一個內部類物件,然後通過這個內部類物件來訪問。

 1 public class Outer {
 2     private double radius=0;
 3     private static String name="Outer";
 4     private Inner inner;
 5     public Outer(double radius){
 6         this.radius=radius;
 7     }
 8     public void getInnerProperty(){
 9         System.out.println("getInnerProperty:"+getInnerInstance().innerName);
10     }
11     Inner getInnerInstance(){
12         if (inner==null){
13             return new Inner();
14         }
15         return inner;
16     }
17     //內部類
18      class Inner{
19         private String innerName="Inner";
20         public Inner(){
21 
22         }
23     }
24 }
1 public class MainApp {
2     public static void main(String[]args){
3         Outer outer=new Outer(10);
4         //建立成員內部類的方法
5         Outer.Inner inner=outer.new Inner();
6         //外部類通過內部類例項訪問內部類的屬性
7         outer.getInnerProperty();
8     }
9 }

執行結果:

getInnerProperty:Inner

3、如果外部類和內部類有重名的屬性和方法

當外部類和成員來擁有相同的屬性和方法時,會發生隱藏現象,即預設情況下訪問的時成員內部類的成員。

外部類和內部類擁有同名的屬性:"shape"和同名方法“hello”

 1 public class Outer {
 2     private double radius=0;
 3     private String shape="Circle";
 4     public Outer(double radius){
 5         this.radius=radius;
 6     }
 7 
 8     private void hello(){
 9         System.out.println("Out say hello");
10     }
11     //內部類
12      class Inner{
13         private String shape="Rect";
14         public Inner(){
15 
16         }
17         private void hello(){
18             System.out.println("Inner say hello");
19         }
20         public void visitTest(){
21             hello();
22             System.out.println("shape:"+shape);
23         }
24     }
25 }
1 public class MainApp {
2     public static void main(String[]args){
3         Outer outer=new Outer(10);
4         //建立成員內部類的方法
5         Outer.Inner inner=outer.new Inner();
6         inner.visitTest();
7     }
8 }

執行結果:

Inner say hello shape:Rect

如果要訪問外部類同名的成員,要按照下面的方式訪問

外部類.this.成員變數

外部類.this.成員方法

 1 @Data
 2 @ToString
 3 public class Outer {
 4     private double radius=0;
 5     private String shape="Circle";
 6     public Outer(double radius){
 7         this.radius=radius;
 8     }
 9     private void hello(){
10         System.out.println("Out say hello");
11     }
12     //內部類
13      class Inner{
14         private String shape="Rect";
15         public Inner(){
16 
17         }
18         private void hello(){
19             System.out.println("Inner say hello");
20         }
21         public void visitTestV2(){
22             Outer.this.hello();
23             System.out.println("shape:"+Outer.this.getShape());
24         }
25     }
26 }
1 public class MainApp {
2     public static void main(String[]args){
3         Outer outer=new Outer(10);
4         //建立成員內部類的方法
5         Outer.Inner inner=outer.new Inner();
6         inner.visitTestV2();
7     }
8 }

執行結果:

Out say helloshape:Circle

區域性內部類

區域性內部類是定義在一個方法或者一個作用域內,它的訪問許可權僅在該方法內或作用域內。不可以用private、protected、public修飾。