接口作为方法参数传递
接口作为方法参数的情况是很常见的,经常会碰到。当遇到方法参数为接口类型时,那么该方法要传入一个接口实现类对象。如下代码演示。
package day14;
//接口作为方法参数传递
public interface smoking {
public static final int A=1;
public abstract void smoking();
}
package day14;
//接口的实现类
public class student implements smoking {public void smoking() {
System.out.println("禁止吸烟");
}
}
package day14;
//测试类,接口作为方法参数传递
public class TestSmokingArgument {
public static void main(String[] args) {
//调用nosmoking方法,传递实现类对象
//实现类对象是自己的对象
student p=new student();
nosmoking(p);
nosmoking(new student());//匿名对象