JAVA匿名內(nèi)部類(Anonymous Classes)的具體使用
1.前言
匿名內(nèi)部類在我們JAVA程序員的日常工作中經(jīng)常要用到,但是很多時(shí)候也只是照本宣科地用,雖然也在用,但往往忽略了以下幾點(diǎn):為什么能這么用?匿名內(nèi)部類的語法是怎樣的?有哪些限制?因此,最近,我在完成了手頭的開發(fā)任務(wù)后,查閱了一下JAVA官方文檔,將匿名內(nèi)部類的使用進(jìn)行了一下總結(jié),案例也摘自官方文檔。感興趣的可以查閱官方文檔(https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)。
2.匿名內(nèi)部類
匿名內(nèi)部類可以使你的代碼更加簡潔,你可以在定義一個(gè)類的同時(shí)對(duì)其進(jìn)行實(shí)例化。它與局部類很相似,不同的是它沒有類名,如果某個(gè)局部類你只需要用一次,那么你就可以使用匿名內(nèi)部類(Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.)
本節(jié)包括以下幾個(gè)方面:
- 定義匿名內(nèi)部類
- 匿名內(nèi)部類的語法
- 訪問作用域的局部變量、定義和訪問匿名內(nèi)部類成員
- 匿名內(nèi)部類實(shí)例
2.1 定義匿名內(nèi)部類
首先看下官方文檔中給的例子:
public class HelloWorldAnonymousClasses { /** * 包含兩個(gè)方法的HelloWorld接口 */ interface HelloWorld { public void greet(); public void greetSomeone(String someone); } public void sayHello() { // 1、局部類EnglishGreeting實(shí)現(xiàn)了HelloWorld接口 class EnglishGreeting implements HelloWorld { String name = "world"; public void greet() { greetSomeone("world"); } public void greetSomeone(String someone) { name = someone; System.out.println("Hello " + name); } } HelloWorld englishGreeting = new EnglishGreeting(); // 2、匿名類實(shí)現(xiàn)HelloWorld接口 HelloWorld frenchGreeting = new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } }; // 3、匿名類實(shí)現(xiàn)HelloWorld接口 HelloWorld spanishGreeting = new HelloWorld() { String name = "mundo"; public void greet() { greetSomeone("mundo"); } public void greetSomeone(String someone) { name = someone; System.out.println("Hola, " + name); } }; englishGreeting.greet(); frenchGreeting.greetSomeone("Fred"); spanishGreeting.greet(); } public static void main(String... args) { HelloWorldAnonymousClasses myApp = new HelloWorldAnonymousClasses(); myApp.sayHello(); } }
運(yùn)行結(jié)果為:
1 Hello world
2 Salut Fred
3 Hola, mundo
該例中用局部類來初始化變量englishGreeting,用匿類來初始化變量frenchGreeting和spanishGreeting,兩種實(shí)現(xiàn)之間有明顯的區(qū)別:
1)局部類EnglishGreetin繼承HelloWorld接口,有自己的類名,定義完成之后需要再用new關(guān)鍵字實(shí)例化才可以使用;
2)frenchGreeting、spanishGreeting在定義的時(shí)候就實(shí)例化了,定義完了就可以直接使用;
3)匿名類是一個(gè)表達(dá)式,因此在定義的最后用分號(hào)";"結(jié)束。
2.2 匿名內(nèi)部類的語法
如上文所述,匿名類是一個(gè)表達(dá)式,匿名類的語法就類似于調(diào)用一個(gè)類的構(gòu)建函數(shù)(new HelloWorld()),除些之外,還包含了一個(gè)代碼塊,在代碼塊中完成類的定義,見以下兩個(gè)實(shí)例:
案例一,實(shí)現(xiàn)接口的匿名類:
HelloWorld frenchGreeting = new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } };
案例二,匿名子類(繼承父類):
public class AnimalTest { private final String ANIMAL = "動(dòng)物"; public void accessTest() { System.out.println("匿名內(nèi)部類訪問其外部類方法"); } class Animal { private String name; public Animal(String name) { this.name = name; } public void printAnimalName() { System.out.println(bird.name); } } // 鳥類,匿名子類,繼承自Animal類,可以覆寫父類方法 Animal bird = new Animal("布谷鳥") { @Override public void printAnimalName() { accessTest(); // 訪問外部類成員 System.out.println(ANIMAL); // 訪問外部類final修飾的變量 super.printAnimalName(); } }; public void print() { bird.printAnimalName(); } public static void main(String[] args) { AnimalTest animalTest = new AnimalTest(); animalTest.print(); } }
運(yùn)行結(jié)果:
運(yùn)行結(jié)果:
匿名內(nèi)部類訪問其外部類方法
動(dòng)物
布谷鳥
從以上兩個(gè)實(shí)例中可知,匿名類表達(dá)式包含以下內(nèi)部分:
- 操作符:new;
- 一個(gè)要實(shí)現(xiàn)的接口或要繼承的類,案例一中的匿名類實(shí)現(xiàn)了HellowWorld接口,案例二中的匿名內(nèi)部類繼承了Animal父類;
- 一對(duì)括號(hào),如果是匿名子類,與實(shí)例化普通類的語法類似,如果有構(gòu)造參數(shù),要帶上構(gòu)造參數(shù);如果是實(shí)現(xiàn)一個(gè)接口,只需要一對(duì)空括號(hào)即可;
- 一段被"{}"括起來類聲明主體;
- 末尾的";"號(hào)(因?yàn)槟涿惖穆暶魇且粋€(gè)表達(dá)式,是語句的一部分,因此要以分號(hào)結(jié)尾)。
3.訪問作用域內(nèi)的局部變量、定義和訪問匿名內(nèi)部類成員
匿名內(nèi)部類與局部類對(duì)作用域內(nèi)的變量擁有相同的的訪問權(quán)限。
(1)、匿名內(nèi)部類可以訪問外部內(nèi)的所有成員;
(2)、匿名內(nèi)部類不能訪問外部類未加final修飾的變量(注意:JDK1.8即使沒有用final修飾也可以訪問);
(3)、屬性屏蔽,與內(nèi)嵌類相同,匿名內(nèi)部類定義的類型(如變量)會(huì)屏蔽其作用域范圍內(nèi)的其他同名類型(變量):
案例一,內(nèi)嵌類的屬性屏蔽:
public class ShadowTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) { System.out.println("x = " + x); System.out.println("this.x = " + this.x); System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); } } public static void main(String... args) { ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.new FirstLevel(); fl.methodInFirstLevel(23); } }
輸出結(jié)果為:
x = 23
this.x = 1
ShadowTest.this.x = 0
這個(gè)實(shí)例中有三個(gè)變量x:1、ShadowTest類的成員變量;2、內(nèi)部類FirstLevel的成員變量;3、內(nèi)部類方法methodInFirstLevel的參數(shù)。
methodInFirstLevel的參數(shù)x屏蔽了內(nèi)部類FirstLevel的成員變量,因此,在該方法內(nèi)部使用x時(shí)實(shí)際上是使用的是參數(shù)x,可以使用this關(guān)鍵字來指定引用是成員變量x:
System.out.println("this.x = " + this.x);
利用類名來引用其成員變量擁有最高的優(yōu)先級(jí),不會(huì)被其他同名變量屏蔽,如:
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
案例二,匿名內(nèi)部類的屬性屏蔽:
public class ShadowTest { public int x = 0; interface FirstLevel { void methodInFirstLevel(int x); } FirstLevel firstLevel = new FirstLevel() { public int x = 1; @Override public void methodInFirstLevel(int x) { System.out.println("x = " + x); System.out.println("this.x = " + this.x); System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); } }; public static void main(String... args) { ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.firstLevel; fl.methodInFirstLevel(23); } }
輸出結(jié)果為:
x = 23
this.x = 1
ShadowTest.this.x = 0
(4)、匿名內(nèi)部類中不能定義靜態(tài)屬性、方法;
public class ShadowTest { public int x = 0; interface FirstLevel { void methodInFirstLevel(int x); } FirstLevel firstLevel = new FirstLevel() { public int x = 1; public static String str = "Hello World"; // 編譯報(bào)錯(cuò) public static void aa() { // 編譯報(bào)錯(cuò) } public static final String finalStr = "Hello World"; // 正常 public void extraMethod() { // 正常 // do something } }; }
(5)、匿名內(nèi)部類可以有常量屬性(final修飾的屬性);
(6)、匿名內(nèi)部內(nèi)中可以定義屬性,如上面代碼中的代碼:private int x = 1;
(7)、匿名內(nèi)部內(nèi)中可以可以有額外的方法(父接口、類中沒有的方法);
(8)、匿名內(nèi)部內(nèi)中可以定義內(nèi)部類;
(9)、匿名內(nèi)部內(nèi)中可以對(duì)其他類進(jìn)行實(shí)例化。
4.匿名內(nèi)部類實(shí)例
官方提供的兩個(gè)實(shí)例供大家參考:
實(shí)例一:
import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }
實(shí)例二:
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class CustomTextFieldSample extends Application { final static Label label = new Label(); @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 300, 150); stage.setScene(scene); stage.setTitle("Text Field Sample"); GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(5); grid.setHgap(5); scene.setRoot(grid); final Label dollar = new Label("$"); GridPane.setConstraints(dollar, 0, 0); grid.getChildren().add(dollar); final TextField sum = new TextField() { @Override public void replaceText(int start, int end, String text) { if (!text.matches("[a-z, A-Z]")) { super.replaceText(start, end, text); } label.setText("Enter a numeric value"); } @Override public void replaceSelection(String text) { if (!text.matches("[a-z, A-Z]")) { super.replaceSelection(text); } } }; sum.setPromptText("Enter the total"); sum.setPrefColumnCount(10); GridPane.setConstraints(sum, 1, 0); grid.getChildren().add(sum); Button submit = new Button("Submit"); GridPane.setConstraints(submit, 2, 0); grid.getChildren().add(submit); submit.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { label.setText(null); } }); GridPane.setConstraints(label, 0, 1); GridPane.setColumnSpan(label, 3); grid.getChildren().add(label); scene.setRoot(grid); stage.show(); } public static void main(String[] args) { launch(args); } }
寫在最后:
這篇文章是我在閱讀官方文檔的同時(shí)加以自己的理解整理出來的,可能受英文原版的影響,有些地方表達(dá)得不準(zhǔn)確或是不清楚還希望讀者能夠指正。另外,體會(huì)到了那些翻譯英文技術(shù)書的人確實(shí)不容易,英文的文章看上去意思都很清楚,但是想要再用中文表述出來卻不那么容易。
到此這篇關(guān)于JAVA匿名內(nèi)部類(Anonymous Classes)的具體使用的文章就介紹到這了,更多相關(guān)JAVA 匿名內(nèi)部類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WIN10環(huán)境 Maven的安裝與配置詳細(xì)教程
這篇文章主要介紹了WIN10環(huán)境 Maven的安裝與配置詳細(xì)教程,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的示例
這篇文章主要介紹了Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的示例,幫助大家方便的進(jìn)行文件格式轉(zhuǎn)換,完成需求,感興趣的朋友可以了解下2020-11-11spring boot 自定義參數(shù)過濾器,把傳入的空字符轉(zhuǎn)換成null方式
這篇文章主要介紹了spring boot 自定義參數(shù)過濾器,把傳入的空字符轉(zhuǎn)換成null方式。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java中ArrayList與順序表的定義與實(shí)現(xiàn)方法
ArrayList是一個(gè)實(shí)現(xiàn)List接口的類,底層是動(dòng)態(tài)類型順序表,本質(zhì)也就是數(shù)組,動(dòng)態(tài)主要體現(xiàn)在它的擴(kuò)容機(jī)制,下面這篇文章主要給大家介紹了關(guān)于Java中ArrayList與順序表的定義與實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-07-07SpringBoot實(shí)現(xiàn)過濾器Filter的三種方式
過濾器Filter由Servlet提供,基于函數(shù)回調(diào)實(shí)現(xiàn)鏈?zhǔn)綄?duì)網(wǎng)絡(luò)請(qǐng)求與響應(yīng)的攔截與修改,本文講給大家詳細(xì)介紹SpringBoot實(shí)現(xiàn)過濾器Filter的三種方式,需要的朋友可以參考下2023-08-08實(shí)體類使用@Builder,導(dǎo)致@ConfigurationProperties注入屬性失敗問題
這篇文章主要介紹了實(shí)體類使用@Builder,導(dǎo)致@ConfigurationProperties注入屬性失敗問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot整合微信登錄功能的實(shí)現(xiàn)方案
今天通過本文給大家分享微信登錄與SpringBoot整合過程,微信掃描登錄實(shí)現(xiàn)代碼知道掃描后點(diǎn)擊登錄的全部過程,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10Java使用OpenCV3.2實(shí)現(xiàn)視頻讀取與播放
這篇文章主要為大家詳細(xì)介紹了Java使用OpenCV3.2實(shí)現(xiàn)視頻讀取與播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07java對(duì)xml節(jié)點(diǎn)屬性的增刪改查實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava對(duì)xml節(jié)點(diǎn)屬性的增刪改查實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10