mirror of https://github.com/Black-Gold/Learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
583 B
46 lines
583 B
public class Jb36_AnsInnerClass2
|
|
{
|
|
/*
|
|
匿名内部类使用场景之一:当函数参数是接口类型,而且接口中的方法不超过三个
|
|
可以作为匿名内部类作为实际参数传递
|
|
*/
|
|
public static void main(String[] args)
|
|
{
|
|
show(new Inf()
|
|
{
|
|
public void show1()
|
|
{}
|
|
public void show2()
|
|
{}
|
|
});
|
|
}
|
|
|
|
public static void show(Inf in)
|
|
{
|
|
in.show1();
|
|
in.show2();
|
|
}
|
|
}
|
|
|
|
interface Inf
|
|
{
|
|
void show1();
|
|
void show2();
|
|
}
|
|
|
|
//匿名内部类的使用
|
|
class Outer
|
|
{
|
|
public void method()
|
|
{
|
|
Inf in = new Inf()
|
|
{
|
|
public void show1()
|
|
{}
|
|
public void show2()
|
|
{}
|
|
};
|
|
in.show1();
|
|
in.show2();
|
|
}
|
|
} |