黑马程序员技术交流社区
标题:
匿名对象可以这么玩—无限循环调用
[打印本页]
作者:
as604049322
时间:
2014-12-17 10:12
标题:
匿名对象可以这么玩—无限循环调用
下面一点简单的代码几乎包涵面向对象70%的知识哦。
class Extends
{
public static void main(String[] args)
{
new Runner(){
public void getSum(int a,int b){
System.out.println(a+b);
}
}.getSum(2,3);
new Pelope(){
public Pelope shot(){
System.out.println("喊一声");
return this;
}
}.shot().getSum(1,3);
new SuperPelope().shot().shot().shot().shot()
.shot().shot().shot().shot().shot().shot()
.shot().shot().shot().shot().shot().getSum(SuperPelope.count,0);
//可以一直喊下去。
//也可以通过for循环控制喊叫次数。。。
SuperPelope.count=0;
SuperPelope pe=new SuperPelope();
for(int i=0;i<20;i++){
pe=(SuperPelope)pe.shot();
}
}
}
interface Runner
{
void getSum(int a,int b);
}
abstract class Pelope implements Runner{
public void getSum(int a,int b){
System.out.println(a+b);
}
abstract Pelope shot();
}
class SuperPelope extends Pelope
{
static int count=0;
public Pelope shot(){
System.out.println("喊"+(++count)+"声!");
return this;
}
}
复制代码
作者:
我只是一只菜鸟
时间:
2014-12-17 10:22
是匿名内部类吧?
作者:
as604049322
时间:
2014-12-17 12:08
我只是一只菜鸟 发表于 2014-12-17 10:22
是匿名内部类吧?
额。。我的用词不专业
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2