java interface 实例_详解java中interface接口的代码案例

这篇文章主要介绍了 java中的interface接口实例详解的相关资料,需要的朋友可以参考下

java中的interface接口实例详解

接口:Java接口是一些方法表征的集合,但是却不会在接口里实现具体的方法。

java接口的特点如下:

1、java接口不能被实例化

2、java接口中声明的成员自动被设置为public,所以不存在private成员

3、java接口中不能出现方法的具体实现。

4、实现某个接口就必须要实现里面定义的所有方法。

接下来看一个实现接口的案例:package hello;

interface competer{ //定义接口

void set_compt(int com);

void print_compt_information();

}

class bj implements competer{ //接口实现

int com ;

public void set_compt(int com)

{

this.com = com ;

// System.out.println("端口" + com);

}

public void print_compt_information()

{

System.out.println("端口" + com);

System.out.println("笔记本");

}

}

class taishi implements competer{

int com ;

public void set_compt(int com)

{

this.com = com ;

//System.out.println("端口" + com);

}

public void print_compt_information()

{

System.out.println("端口" + com);

System.out.println("台式机");

}

}

public class inter {

public static void main(String[] args)

{

taishi com = new taishi();

bj bjj = new bj();

com.set_compt(100);

bjj.set_compt(120);

com.print_compt_information();

bjj.print_compt_information();

}

}

运行结果:端口100

台式机

端口120

笔记本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值