日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java学习面向对象之内部类

發(fā)布時間:2025/4/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java学习面向对象之内部类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

什么是面向?qū)ο髢?nèi)部類呢?所謂的內(nèi)部類,即從字面意義上來理解的話,就是把類放到類當中。

那么內(nèi)部類都有什么特點呢?

1、內(nèi)部類可以訪問包裹他的類的成員。

2、如果包裹他的類想訪問被其包裹的類的話就得實例化,才可以訪問。

代碼示例:

1 class Outer 2 { 3 4 int num = 10; 5 6 void show() 7 { 8 9 System.out.println("Outer Show method!!"); 10 11 } 12 13 void method() 14 { 15 16 Inner a = new Inner(); 17 a.innerShow(); 18 19 } 20 21 22 static class Inner 23 { 24 25 static final int num = 20; 26 27 void innerShow() 28 { 29 30 System.out.println("The num is "+num); 31 //show(); 32 staticShow(); 33 34 } 35 36 static void staticShow() 37 { 38 39 System.out.println("Static Inner Show"); 40 41 } 42 43 } 44 45 } 46 47 class Inner 48 { 49 50 51 void innerShow() 52 { 53 54 System.out.println("The num is "); 55 56 } 57 58 } 59 60 class InnerClassDemo1 61 { 62 63 64 public static void main(String[] args) { 65 66 Outer.Inner out = new Outer.Inner(); 67 out.innerShow(); 68 69 } 70 71 }

內(nèi)部類的作用:

  一般用來作為類的設(shè)計。

  當我們描述事物的時候,發(fā)現(xiàn)其中還有事物,并且這個事物還在訪問被描述事物的內(nèi)容。這個時候就用到了內(nèi)部類。

?內(nèi)部類還有哪些特點呢?

1、內(nèi)部類中有靜態(tài)變量的話,內(nèi)部類必須是靜態(tài)的。也就是說內(nèi)部類可以被修飾符修飾,因為他本身也是類內(nèi)部的成員。

2、如果想從外部直接調(diào)用內(nèi)部類,必須按照這種方式來調(diào)用:

1 class InnerClassDemo1 2 { 3 4 public static void main(String[] args) { 5 6 Outer.Inner inner = new Outer().new Inner(); 7 inner.showInner(); 8 9 } 10 11 }

內(nèi)部類細節(jié)注意:

1、this變量,我們知道一個類對象被new之后肯定有一個this與之相關(guān)聯(lián),那么類當中有內(nèi)部類的時候,如果單憑一個this的話可能就會造成混淆。我們來寫段代碼來測試一下:

1 class Outer 2 { 3 4 private int num = 10; 5 6 class Inner 7 { 8 int num = 15; 9 void showInner() 10 { 11 12 System.out.println("The Outer num is " + this.num); 13 System.out.println("The Inner num is " + Inner.this.num); 14 system.out.println("The Oute num is "+Outer.this.num); 15 16 } 17 18 } 19 20 void method() 21 { 22 23 Inner inner = new Inner(); 24 inner.showInner(); 25 26 } 27 28 } 29 30 class InnerClassDemo1 31 { 32 33 public static void main(String[] args) { 34 35 Outer.Inner inner = new Outer().new Inner(); 36 inner.showInner(); 37 38 } 39 40 }

?

2、為什么內(nèi)部類能夠直接訪問外部類的成員呢,因為內(nèi)部類持有外部類的引用,書寫格式就是外部類.this.

?

內(nèi)部類的另外一個特點就是,可以放在局部位置上,什么是局部位置呢,形象一點來說的話,就是可以放到函數(shù)當中。示例:

1 class Outer 2 { 3 4 private int num = 10; 5 6 void method() 7 { 8      int num = 9;//必須用final關(guān)鍵字修飾之后,在Inner但中才可以訪問這個值。 9 class Inner 10 { 11 //int x = 9; 12 void show() 13 { 14 15 System.out.println("the num is :"+num); 16 17 } 18 19 } 20 21 Inner inner = new Inner(); 22 inner.show(); 23 24 } 25 26 } 27 28 class InnerClassDemo1 29 { 30 31 public static void main(String[] args) { 32 33 Outer out = new Outer(); 34 out.method(); 35 36 } 37 38 }

內(nèi)部類在局部位置上只能,訪問局部但中由final關(guān)鍵字修飾的局部變量。為什么呢?因為當類在局部的時候,當局部方法進棧之后,變量生成,出棧之后變量消失,如果在方法外調(diào)用局部作用域的話,因為方法已經(jīng)出棧了,那么局部變量已經(jīng)消失了,釋放了。此時若要保證外部能夠訪問局部位置上的內(nèi)部類中的調(diào)用了局部位置的變量的方法,只能夠把局部位置上的局部變量用final修飾成常量,這樣才可以供外部訪問。java是一門嚴謹性的語言。

轉(zhuǎn)載于:https://www.cnblogs.com/sunchuanzhen/p/3353511.html

總結(jié)

以上是生活随笔為你收集整理的java学习面向对象之内部类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。