首頁 > Java > java教程 > 主體

詳解java中interface介面的程式碼案例

黄舟
發布: 2017-03-28 10:30:13
原創
1732 人瀏覽過

這篇文章主要介紹了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
笔记本
登入後複製

以上是詳解java中interface介面的程式碼案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!