Home >Java >javaTutorial >Detailed explanation of code examples of interface interface in java

Detailed explanation of code examples of interface interface in java

黄舟
黄舟Original
2017-03-28 10:30:131874browse

This article mainly introduces relevant information about the detailed explanation of interface interface instances in java. Friends who need it can refer to

Detailed explanation of interface interface instances in java

Interface: Java interface is a collection of method representations, but specific methods are not implemented in the interface.

The characteristics of the java interface are as follows:

1. The java interface cannot be instantiated.
2. The members declared in the java interface are automatically It is set to public, so there is no private member
3. The specific implementation of the method cannot appear in the java interface.
4. To implement an interface, you must implement all the methods defined in it.

Let’s look at a case of implementing the interface:

 
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(); 
  } 
}

Running result:

端口100
台式机
端口120
笔记本

The above is the detailed content of Detailed explanation of code examples of interface interface in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn