什么是匿名內(nèi)部類什么時(shí)侯用
Anonymous inner class
馬克-to-win:有時(shí)如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,用一下就完,就用匿名內(nèi)部類。注意: 下面的new FigureMark_to_win(){。。。。};的語(yǔ)法形式。它和以往的new FigureMark_to_win()不同,現(xiàn)在的這個(gè)new的是FigureMark_to_win的子類(否則“我啥也不是”, 應(yīng)該被打印出來(lái)。)。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
例2.7_0:
class FigureMark_to_win {
void whoAmI(){
System.out.println("我啥也不是");
}
}
class Triangle extends FigureMark_to_win {
void whoAmI() {
System.out.println("三角形!");
}
}
public class Test {
public static void main(String[] args) {
Triangle d = new Triangle();
d.whoAmI();
new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒(méi)有像上面一樣提出一個(gè)Triangle類的概念,主要是如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。}; ,馬克-to-win: 大家注意一下格式和分號(hào)。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
FigureMark_to_win a = new FigureMark_to_win() {
void whoAmI() {
System.out.println("長(zhǎng)方形!");
}
};
a.whoAmI();
new FigureMark_to_win() {
void whoAmI() {
System.out.println("圓形");
}
}.whoAmI();//連指針都省了
}
}
結(jié)果:
三角形!
三角形!
長(zhǎng)方形!
圓形
匿名內(nèi)部類的父類也可以是抽象類或接口。以下給出例子:
什么是匿名內(nèi)部類什么時(shí)侯用
Anonymous inner class
馬克-to-win:有時(shí)如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,用一下就完,就用匿名內(nèi)部類。注意: 下面的new FigureMark_to_win(){。。。。};的語(yǔ)法形式。它和以往的new FigureMark_to_win()不同,現(xiàn)在的這個(gè)new的是FigureMark_to_win的子類(否則“我啥也不是”, 應(yīng)該被打印出來(lái)。)。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
例2.7_0:
class FigureMark_to_win {
void whoAmI(){
System.out.println("我啥也不是");
}
}
class Triangle extends FigureMark_to_win {
void whoAmI() {
System.out.println("三角形!");
}
}
public class Test {
public static void main(String[] args) {
Triangle d = new Triangle();
d.whoAmI();
new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒(méi)有像上面一樣提出一個(gè)Triangle類的概念,主要是如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。}; ,馬克-to-win: 大家注意一下格式和分號(hào)。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
FigureMark_to_win a = new FigureMark_to_win() {
void whoAmI() {
System.out.println("長(zhǎng)方形!");
}
};
a.whoAmI();
new FigureMark_to_win() {
void whoAmI() {
System.out.println("圓形");
}
}.whoAmI();//連指針都省了
}
}
結(jié)果:
三角形!
三角形!
長(zhǎng)方形!
圓形
匿名內(nèi)部類的父類也可以是抽象類或接口。以下給出例子:
例2.7---本章源碼
abstract class FigureMark_to_win {
abstract void whoAmI();
}
class Triangle extends FigureMark_to_win {
void whoAmI() {
System.out.println("三角形!");
}
}
public class Test {
public static void main(String[] args) {
Triangle d = new Triangle();
d.whoAmI();
new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒(méi)有像上面一樣提出一個(gè)Triangle類的概念,主要是如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。},馬克-to-win: 大家注意一下格式和分號(hào)。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
FigureMark_to_win a = new FigureMark_to_win() {
void whoAmI() {
System.out.println("長(zhǎng)方形!");
}
};
a.whoAmI();
new FigureMark_to_win() {
void whoAmI() {
System.out.println("圓形");
}
}.whoAmI();//連指針都省了
}
}
result is:
三角形!
三角形!
長(zhǎng)方形!
圓形
例2.7---本章源碼
abstract class FigureMark_to_win {
abstract void whoAmI();
}
class Triangle extends FigureMark_to_win {
void whoAmI() {
System.out.println("三角形!");
}
}
public class Test {
public static void main(String[] args) {
Triangle d = new Triangle();
d.whoAmI();
new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒(méi)有像上面一樣提出一個(gè)Triangle類的概念,主要是如此簡(jiǎn)單,都沒(méi)有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。},馬克-to-win: 大家注意一下格式和分號(hào)。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
FigureMark_to_win a = new FigureMark_to_win() {
void whoAmI() {
System.out.println("長(zhǎng)方形!");
}
};
a.whoAmI();
new FigureMark_to_win() {
void whoAmI() {
System.out.println("圓形");
}
}.whoAmI();//連指針都省了
}
}
result is:
三角形!
三角形!
長(zhǎng)方形!
圓形