加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

接口作为方法的参数和返回值

发布时间:2021-05-20 09:43:38 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:接口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("请不要吸

接口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);
?? ?}
}



(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读