匿名内部类
匿名内部类的本质:对象
废话不多说,直接上代码
//跳高接口
public interface Jumpping {
public abstract void jump();
}
//跳高操作实现类
public class JumppingOperator {
public void method(Jumpping jumpping){
jumpping.jump();
}
}
//测试类
public class JumppingDemo {
public static void main(String[] args) {
JumppingOperator jumppingOperator = new JumppingOperator();
/* 常规方法:
Jumpping cat = new Cat();
jumppingOperator.method(cat);*/
//内部匿名类方法:
jumppingOperator.method(new Jumpping() {
@Override
public void jump() {
System.out.println("你要跳舞吗");
}
});
}
}
//常规方法的猫类
public class Cat implements Jumpping{
@Override
public void jump() {
System.out.println("猫可以跳很高");
}public class JumppingDemo {
public static void main(String[] args) {
JumppingOperator jumppingOperator = new JumppingOperator();
/* 常规方法:
Jumpping cat = new Cat();
jumppingOperator.method(cat);*/
//内部匿名类方法:
jumppingOperator.method(new Jumpping() {
@Override
public void jump() {
System.out.println("你要跳舞吗");
}
});
}
}
}
内部匿名类代码说明:
jumppingOperator.method(new Jumpping() { //.method()括号内为对象,采用匿名内部类的方法创建对象
@Override
public void jump() {
System.out.println("你要跳舞吗");
}
});