java外部類_Java里什么叫內部類什么叫外部類

展開全部

對普通類(沒有內部類的類)來說,62616964757a686964616fe78988e69d8331333337396234內部類和外部類都與他無關;對有內部類的類來說,它們就是其內部類的外部類,外部類是個相對的說法,其實就是有內部類的類。

所以,要回答這個問題,只需要講解內部類是什么:

Java中的內部類共分為四種:

靜態內部類static inner class (also called nested class)

成員內部類member inner class

局部內部類local inner class

匿名內部類anonymous inner class

靜態內部類Static Inner Class

最簡單的內部類形式。

類定義時加上static關鍵字。

不能和外部類有相同的名字。

被編譯成一個完全獨立的.class文件,名稱為OuterClass$InnerClass.class的形式。

只可以訪問外部類的靜態成員和靜態方法,包括了私有的靜態成員和方法。

生成靜態內部類對象的方式為:

OuterClass.InnerClass inner = new OuterClass.InnerClass();

示例代碼:

package com.learnjava.innerclass;

class StaticInner

{

private static int a = 4;

// 靜態內部類

public static class Inner

{

public void test()

{

// 靜態內部類可以訪問外部類的靜態成員

// 并且它只能訪問靜態的

System.out.println(a);

}

}

}

public class StaticInnerClassTest

{

public static void main(String[] args)

{

StaticInner.Inner inner = new StaticInner.Inner();

inner.test();

}

}

成員內部類Member Inner Class

成員內部類也是定義在另一個類中,但是定義時不用static修飾。

成員內部類和靜態內部類可以類比為非靜態的成員變量和靜態的成員變量。

成員內部類就像一個實例變量。

它可以訪問它的外部類的所有成員變量和方法,不管是靜態的還是非靜態的都可以。

在外部類里面創建成員內部類的實例:

this.new Innerclass();

在外部類之外創建內部類的實例:

(new Outerclass()).new Innerclass();

在內部類里訪問外部類的成員:

Outerclass.this.member

示例代碼:

package com.learnjava.innerclass;

class MemberInner

{

private int d = 1;

private int a = 2;

// 定義一個成員內部類

public class Inner2

{

private int a = 8;

public void doSomething()

{

// 直接訪問外部類對象

System.out.println(d);

System.out.println(a);// 直接訪問a,則訪問的是內部類里的a

// 如何訪問到外部類里的a呢?

System.out.println(MemberInner.this.a);

}

}

}

public class MemberInnerClassTest

{

public static void main(String[] args)

{

// 創建成員內部類的對象

// 需要先創建外部類的實例

MemberInner.Inner2 inner = new MemberInner().new Inner2();

inner.doSomething();

}

}

局部內部類Local Inner Class

局部內部類定義在方法中,比方法的范圍還小。是內部類中最少用到的一種類型。

像局部變量一樣,不能被public, protected, private和static修飾。

只能訪問方法中定義的final類型的局部變量。

局部內部類在方法中定義,所以只能在方法中使用,即只能在方法當中生成局部內部類的實例并且調用其方法。

示例代碼:

package com.learnjava.innerclass;

class LocalInner

{

int a = 1;

public void doSomething()

{

int b = 2;

final int c = 3;

// 定義一個局部內部類

class Inner3

{

public void test()

{

System.out.println("Hello World");

System.out.println(a);

// 不可以訪問非final的局部變量

// error: Cannot refer to a non-final variable b inside an inner

// class defined in a different method

// System.out.println(b);

// 可以訪問final變量

System.out.println(c);

}

}

// 創建局部內部類的實例并調用方法

new Inner3().test();

}

}

public class LocalInnerClassTest

{

public static void main(String[] args)

{

// 創建外部類對象

LocalInner inner = new LocalInner();

// 調用外部類的方法

inner.doSomething();

}

}

匿名內部類Anonymous Inner Class

匿名內部類就是沒有名字的局部內部類,不使用關鍵字class, extends, implements, 沒有構造方法。

匿名內部類隱式地繼承了一個父類或者實現了一個接口。

匿名內部類使用得比較多,通常是作為一個方法參數。

生成的.class文件中,匿名類會生成OuterClass$1.class文件,數字根據是第幾個匿名類而類推。

示例代碼:

package com.learnjava.innerclass;

import java.util.Date;

public class AnonymouseInnerClass

{

@SuppressWarnings("deprecation")

public String getDate(Date date)

{

return date.toLocaleString();

}

public static void main(String[] args)

{

AnonymouseInnerClass test = new AnonymouseInnerClass();

// 打印日期:

String str = test.getDate(new Date());

System.out.println(str);

System.out.println("----------------");

// 使用匿名內部類

String str2 = test.getDate(new Date()

{

});// 使用了花括號,但是不填入內容,執行結果和上面的完全一致

// 生成了一個繼承了Date類的子類的對象

System.out.println(str2);

System.out.println("----------------");

// 使用匿名內部類,并且重寫父類中的方法

String str3 = test.getDate(new Date()

{

// 重寫父類中的方法

@Override

@Deprecated

public String toLocaleString()

{

return "Hello: " + super.toLocaleString();

}

});

System.out.println(str3);

}

}

2Q==

已贊過

已踩過<

你對這個回答的評價是?

評論

收起

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541465.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541465.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541465.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《精通Matlab數字圖像處理與識別》一6.2 傅立葉變換基礎知識

本節書摘來自異步社區《精通Matlab數字圖像處理與識別》一書中的第6章&#xff0c;第6.2節&#xff0c;作者 張錚 , 倪紅霞 , 苑春苗 , 楊立紅&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 6.2 傅立葉變換基礎知識 精通Matlab數字圖像處理與識別要理解傅立…

多線程循環輸出abcc++_C ++循環| 查找輸出程序| 套裝5

多線程循環輸出abccProgram 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int num 15673;int R1 0, R2 0;do {R1 num % 10;R2 R2 * 10 R1;num num / 10;} while (num > 0);cout << R2 << " ";return 0;}Ou…

java oql_深入理解java虛擬機(八):java內存分析工具-MAT和OQL

以下內容翻譯自MAT幫助文檔。一、Class HistogramClass Histogram shows the classes found in the snapshot, the number of objects for each class, the heap memory consumption of these objects, and the minimum retained size of the objects二、Dominator treeDomina…

《Python數據分析與挖掘實戰》一1.2 從餐飲服務到數據挖掘

本節書摘來自華章出版社《Python數據分析與挖掘實戰》一書中的第1章&#xff0c;第1.2節&#xff0c;作者 張良均 王路 譚立云 蘇劍林&#xff0c;更多章節內容可以訪問云棲社區“華章計算機”公眾號查看 1.2 從餐飲服務到數據挖掘 企業經營最大的目的就是盈利&#xff0c;而餐…

obj[]與obj._Ruby中帶有示例的Array.include?(obj)方法

obj[]與obj.Ruby Array.include&#xff1f;(obj)方法 (Ruby Array.include?(obj) Method) In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <> operator, operator, and .eql? method?…

java javah_Java開發網 - 一個javah的問題

Posted by:jerry_xuPosted on:2006-03-13 15:39我在環境變量中已經設置了path為D:\Program Files\Java\jdk1.5.0_06&#xff0c;ClassPath設置為.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;class的路徑為&#xff1a;D:\JNItest\bin\jni\Hello.class &#xff0c;但是…

《Python面向對象編程指南》——2.7 __del__()方法

本節書摘來自異步社區《Python面向對象編程指南》一書中的第2章&#xff0c;第2.7節&#xff0c;作者&#xff3b;美&#xff3d;Steven F. Lott&#xff0c; 張心韜 蘭亮 譯&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 2.7 __del__()方法 __del__()方…

NullReferenceException C#中的異常

什么是NullReferenceException&#xff1f; (What is NullReferenceException?) NullReferenceException is an exception and it throws when the code is trying to access a reference that is not referencing to any object. If a reference variable/object is not refe…

java map key 大寫轉小寫_Spring JdbcTemplate 查詢出的Map,是如何產生大小寫忽略的Key的?(轉)...

Java 是區分大小寫的&#xff0c;普通的Map例如HashMap如果其中的key"ABC" value"XXX"那么map.get("Abc") 或 map.get("abc")是獲取不到值得。但Spring中產生了一個忽略大小寫的map使我產生了好奇例如 jdbcTemplate.queryForList(sql)…

《iOS 6核心開發手冊(第4版)》——2.11節秘訣:構建星星滑塊

本節書摘來自異步社區《iOS 6核心開發手冊&#xff08;第4版&#xff09;》一書中的第2章&#xff0c;第2.11節秘訣&#xff1a;構建星星滑塊&#xff0c;作者 【美】Erica Sadun&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 2.11 秘訣&#xff1a;構建星星…

css框架和js框架_優雅設計的頂級CSS框架

css框架和js框架Brief discussion: 簡要討論&#xff1a; Well, who doesnt want their website or web page to look attractive, stylish and be responsive? 那么&#xff0c;誰不希望自己的網站或網頁看起來有吸引力&#xff0c;時尚并且ReactSwift&#xff1f; We put …

軟考下午題具體解釋---數據流圖設計

在歷年的軟考下午題其中&#xff0c;有五道大題。各自是數據流圖的設計&#xff0c;數據庫設計&#xff0c;uml圖&#xff0c;算法和設計模式&#xff0c;從今天這篇博文開始&#xff0c;小編就跟大家來一起學習軟考下午題的相關內容。包含理論上的知識以及典型例題的解說&…

基本程序 打印Scala的Hello World

Scala中的基本程序 (Basic program in Scala) As your first Scala program, we will see a basic output program that just prints "Hello World" or any other similar type of string. With this example, we will see what are the part of the code that is im…

java treemap lastkey_Java TreeMap lastKey()用法及代碼示例

java.util.TreeMap.lastKey()用于檢索Map中存在的最后一個或最高鍵。用法:tree_map.lastKey()參數&#xff1a;該方法不帶任何參數。返回值&#xff1a;該方法返回映射中存在的最后一個鍵。異常&#xff1a;如果映射為空&#xff0c;則該方法將引發NoSuchElementException。以下…

mysql屬于數據庫三級模式_數據庫系統的三級模式指的是什么

數據庫系統的三級模式指的是什么發布時間&#xff1a;2020-10-26 10:11:21來源&#xff1a;億速云閱讀&#xff1a;52作者&#xff1a;小新小編給大家分享一下數據庫系統的三級模式指的是什么&#xff0c;希望大家閱讀完這篇文章后大所收獲&#xff0c;下面讓我們一起去探討吧&…

《自頂向下網絡設計(第3版)》——導讀

目錄 第1部分 辨明客戶的需求和目標 第1章 分析商業目標和制約 1.1 采用自頂向下的網絡設計方法 1.2 分析商業目標 1.3 分析商業制約 1.4 商業目標檢查表 1.5 小結 1.6 復習題 1.7 設計環境 第2章 分析技術目標與折衷措施 2.1 可擴展性 2.2 可用性 2.3 網絡性能 2.4 安全性 2…

python矩陣變化_用numpy改變矩陣的形狀

我的問題有兩個方面。我有下面的代碼來處理一些矩陣。在import numpytupleList [(0, 122), (1, 246), (2, 157), (3, 166), (4, 315), (5, 108), (6, 172), (7, 20), (8, 173), (9, 38), (10, 28), (11, 72), (12, 102), (13, 277), (14, 318), (15, 316), (16, 283), (17, 31…

最小硬幣問題_進行更改的最小硬幣數量

最小硬幣問題Description: 描述&#xff1a; This is classic dynamic programming problem to find minimum number of coins to make a change. This problem has been featured in interview rounds of Amazon, Morgan Stanley, Paytm, Samsung etc. 這是經典的動態編程問題…

java 生成xml亂碼_jdom解決中文亂碼問題 JAVA生成xml文件幫了我很大的忙

決解了數據庫讀取出來 再保存到xml 產生的亂碼問題import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import org.jdom.Attribute;import org.jdom.Document;import org.jdom.Element;import org.jdom.output.Format;import org.…

給定重量上限,背包問題_滿足給定重量的袋子的最低成本

給定重量上限,背包問題Problem statement: 問題陳述&#xff1a; You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of i kg packet of oranges. cost[i] -1 means t…