[可见度] interface 接口名称 [extends 其他的接口名] { // 声明变量 // 抽象方法 }
import java.lang.*; //引入包 public interface NameOfInterface { //任何类型 final, static 字段 //抽象方法 }
interface Animal { public void eat(); public void travel(); }
public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int noOfLegs(){ return 0; } public static void main(String args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } }
public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); } // 文件名: Football.java public interface Football extends Sports { public void homeTeamScored(int points); public void visitingTeamScored(int points); public void endOfQuarter(int quarter); } // 文件名: Hockey.java public interface Hockey extends Sports { public void homeGoalScored(); public void visitingGoalScored(); public void endOfPeriod(int period); public void overtimePeriod(int ot); }
public interface Hockey extends Sports, Event
package java.util; public interface EventListener {}