接口Smoking:
public interface Smoking { ?? ?public static final int a=1; ?? ?public abstract void smoking();
}
Student类实现接口:
public class Student implements Smoking { ?? ?public void smoking() { ?? ??? ?System.out.println("请不要吸烟"); ?? ?}
}
测试TestArguments类:
public class TestArguments { ?? ?public static void main(String[] args) { ?? ??? ?//调用noSmoking方法,传递实现类对象 ?? ??? ?//实现类对象时自己的对象 ?? ??? ?Student s=new Student(); ?? ??? ?noSmoking(s); ?? ??? ? ?? ??? ?noSmoking(new Student()); ?? ??? ? ?? ??? ?//实现类对象,是采用多态创建出来的 ?? ??? ?Smoking t=new Student(); ?? ??? ?noSmoking(t); ?? ?} ?? ?/* ?? ? * 定义方法,方法参数写成接口类型 ?? ? */ ?? ?public static void noSmoking(Smoking s){ ?? ??? ?//接口的引用变量s,调用方法 ?? ??? ?s.smoking(); ?? ??? ?//调用接口中的常量a ?? ??? ?System.out.println(Smoking.a); ?? ?} }
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|