- 多个无关的类可以实现同一个接口。
- 一个类可以实现多个无关的接口。
- 与继承关系类似,接口与实现类之间存在多态性。
- 定义Java类的语法格式:
<modifier> class <name> [extends <superclass>] [implements <interface> [,<interface>]*]{ <declarations>* }
实现现实世界中多继承的现象,因为Java只能单继承。
- 接口(interface)是抽象方法和常量值的定义的集合。
- 从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现。
- 接口定义举例:
public interface Runner{ public static final int id = 1; public void start(); public void run(); public void stop(); }
接口特性
- 接口可以多重实现;
- 接口中声明的属性默认为public static final的;也只能是public static final的;
- 接口中只能定义抽象方法,而且这些方法默认为public的,也只能是public的;
- 接口可以继承其它的接口,并添加新的属性和抽象方法。
示例1:
package com.nyist; interface Singer{ public void sing(); public void sleep(); } class Student implements Singer{ private String name; Student(String name){ this.name = name; } public void study() { System.out.println("studying"); } public String getName() { return name; } public void sing() { System.out.println("student is singing"); } public void sleep() { System.out.println("student is sleeping"); } } public class TestInterface { public static void main(String[] args) { Student s1 = new Student("小明"); System.out.println(s1.getName()); } }
示例2:
package com.nyist; interface Singer{ public void sing(); public void sleep(); } interface Painter{ public void paint(); public void eat(); } class Student implements Singer{ private String name; Student(String name){ this.name = name; } public void study() { System.out.println("studying"); } public String getName() { return name; } public void sing() { System.out.println("student is singing"); } public void sleep() { System.out.println("student is sleeping"); } } class Teacher implements Singer,Painter{ private String name; public String getString() { return name; } Teacher(String name){ this.name = name; } public void teach() { System.out.println("teaching"); } public void sing() { System.out.println("singing"); } public void sleep() { System.out.println("sleeping"); } public void paint() { System.out.println("painting"); } public void eat() { System.out.println("eating"); } } public class TestInterface { public static void main(String[] args) { Student s1 = new Student("小明"); System.out.println(s1.getName()); Teacher t1 = new Teacher("王老师"); System.out.println(t1.getString()); } }
标准示例:
package com.nyist; interface Singer{ public void sing(); public void sleep(); } interface Painter{ public void paint(); public void eat(); } class Student implements Singer{ private String name; Student(String name){ this.name = name; } public void study() { System.out.println("studying"); } public String getName() { return name; } public void sing() { System.out.println("student is singing"); } public void sleep() { System.out.println("student is sleeping"); } } class Teacher implements Singer,Painter{ private String name; public String getString() { return name; } Teacher(String name){ this.name = name; } public void teach() { System.out.println("teaching"); } public void sing() { System.out.println("singing"); } public void sleep() { System.out.println("sleeping"); } public void paint() { System.out.println("painting"); } public void eat() { System.out.println("eating"); } } public class TestInterface { public static void main(String[] args) { Singer s1 = new Student("le"); s1.sing(); s1.sleep(); Singer s2 = new Teacher("steven"); s2.sing(); s2.sleep(); Painter p1 = (Painter)s2; p1.paint(); p1.eat(); } }
结果:
student is singing
student is sleeping
singing
sleeping
painting
eating
面向对象总结: