每日一言 Being strong on your own is meaningless. To have power you need other people, and they need a world where they can be at their best. – Shiroe from Log Horizon
Java 接口 Java编程语言中是一个抽象类型,是抽象方法的集合。
接口中的所有方法默认是public abstract 的
接口中的所有变量默认是 public static final
的常量
接口不能被实例化,需要由类来实现
一个类可以实现多个接口
接口可以继承其他接口
接口的声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [访问修饰符] interface <_name> [exdends <name>,<name>,...]{ }public interface InterfaceName { type CONSTANT_NAME = value; returnType methodName (parameterList) ; default returnType methodName (parameterList) { } static returnType methodName (parameterList) { } private returnType methodName (parameterList) { } }
接口的实现 类可以实现接口:
1 2 3 4 5 6 7 public class ClassName implements InterfaceName { @Override public returnType methodName (parameterList) { } }
类实现接口时,必须要实现接口中所有的抽象方法,默认方法可以不实现。否则必须要声明为抽象类 。
java中类可以一次实现多个接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class MyClass implements Interface1 , Interface2, Interface3 { @Override public void method1 () { } @Override public void method2 () { } @Override public void method3 () { } }
接口的继承 接口的继承与类相似,但是可以同时继承多个接口,而类一次只能继承一个父类。
1 public interface Hockey extends Sports , Event