/*
匿名内部类的简要概述
*/
public class Test02 {
public static void main(String[] args) {
MyMath myMath = new MyMath();
//myMath.MySum(new ComputerImp(),100,200); //利用接口的实现类和多态
myMath.MySum(new Compute() { //利用匿名内部类
@Override
public int sum(int x, int y) {
return x+y;
}
},100,200);
}
}
interface Compute{
int sum(int x,int y);
}
class ComputerImp implements Compute{ //使用匿名内部类时,此接口的实现类不写
@Override
public int sum(int x, int y) {
return x + y;
}
}
class MyMath{
public void MySum(Compute compute ,int x ,int y){
int sum = compute.sum(x, y);
System.out.println("求和是:" + sum);
}
}
匿名内部类的简要概述
最新推荐文章于 2025-05-14 14:22:49 发布