老生常談 java匿名內(nèi)部類
匿名內(nèi)部類:
1、匿名內(nèi)部類其實就是內(nèi)部類的簡寫格式。
2、定義匿名內(nèi)部類的前提:
內(nèi)部類必須是繼承一個類或者實現(xiàn)接口。
3、匿名內(nèi)部類的格式: new 父類或者接口(){定義子類的內(nèi)容}
4、其實匿名內(nèi)部類就是一個匿名子類對象。而且這個對象有點胖。 可以理解為帶內(nèi)容的對象。
5、匿名內(nèi)部類中定義的方法最好不要超過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("hehe");
}
}
*/
public void function()
{
//AbsDemo a = new Inner();
// Inner in = new Inner();
// in.show();
// in.abc();
AbsDemo d = new AbsDemo()
{
int num = 9;
void show()
{
System.out.println("num==="+num);
}
void abc()
{
System.out.println("haha");
}
};
d.show();
//d.abc();//編譯失敗;
}
}
class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
}
}
以上這篇老生常談 java匿名內(nèi)部類就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決SpringBoot使用@Value獲取不到y(tǒng)aml中配置值的問題
在最近的開發(fā)中遇到一個問題,使用@Value獲取yml文件中配置的屬性時始終獲取不到值,所以本文給大家詳細介紹了SpringBoot使用@Value獲取不到y(tǒng)aml中值的問題分析及解決方法,需要的朋友可以參考下2024-01-01
eclipse 如何創(chuàng)建 user library 方法詳解
這篇文章主要介紹了eclipse 如何創(chuàng)建 user library 方法詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

