- 子类必须通过implements来实现接口中的规范
- 接口不能创建实例,但是可用于声明引用变量类型
- 一个类实现了接口,必须实现接口中所有方法,并且这些方法只能是public
接口支持多继承
package theChildOfGod.bjxy.oop.testInterface;
public interface Flyable {
int MAX_SPEED = 11000;
int MIN_HEIGHT = 1;
void fly();
}
interface Attack {
void fire();
}
class Plane implements Flyable{
@Override
public void fly() {
System.out.println("飞机依靠发动机在飞");
}
}
class Man implements Flyable, Attack{
@Override
public void fly() {
System.out.println("跳起来,飞!");
}
@Override
public void fire() {
System.out.println("开火!");
}
}
package theChildOfGod.bjxy.oop.testInterface;
public interface MyInterface {
String MAX_GREAD = "BOSS";
int MAX_SPEED = 120;
public void test01();
public int test02(int a, int b);
}
package theChildOfGod.bjxy.oop.testInterface;
public class MyClass implements MyInterface{
@Override
public void test01() {
System.out.println("test01");
}
@Override
public int test02(int a, int b) {
System.out.println("MyClass.test02()");
return a+b;
}
}
package theChildOfGod.bjxy.oop.testInterface;
public interface InterfaceA {
void aaa();
}
interface InterfaceB{
void bbb();
}
interface InterfaceC extends InterfaceA, InterfaceB{
void ccc();
}
class TestClass implements InterfaceC{
@Override
public void aaa() {
}
@Override
public void bbb() {
}
@Override
public void ccc() {
}
}