Java 接口
接口的声明与实现、多接口、default/static 方法与接口继承。
接口的声明
interface declares a capability contract. Methods are implicitly public abstract and fields public static final — writing those modifiers is redundant and often rejected.
interface Animal {
int MAX_AGE = 100; // public static final
void eat(); // public abstract
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("dog eats");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Animal a = new Dog();
a.eat();
System.out.println(Animal.MAX_AGE);
}
}dog eats
100接口的实现
A class implements with implements and must define every abstract method as public — the interface declares them public, so narrowing is illegal.
interface Flyer {
void fly();
}
interface Swimmer {
void swim();
}
class Duck implements Flyer, Swimmer {
@Override
public void fly() {
System.out.println("duck flies");
}
@Override
public void swim() {
System.out.println("duck swims");
}
}
public class MultiImpl {
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
Flyer f = d;
f.fly();
}
}duck flies
duck swims
duck fliesdefault 与 static 方法
Since Java 8 an interface may hold default methods (implemented, overridable) and static methods (callable only via the interface name). This lets interfaces evolve without breaking implementations.
interface Greeter {
void greet();
default void greetTwice() {
greet();
greet();
}
static Greeter of(String name) {
return () -> System.out.println("hi " + name);
}
}
public class DefaultDemo {
public static void main(String[] args) {
Greeter g = Greeter.of("Tom");
g.greetTwice();
}
}hi Tom
hi Tom接口的继承
Interfaces extend with extends and may extend several at once. A child interface inherits abstract and default methods, and implementing classes must fulfil all abstract ones.
interface A {
void a();
}
interface B extends A {
void b();
}
class Impl implements B {
@Override
public void a() {
System.out.println("a");
}
@Override
public void b() {
System.out.println("b");
}
}
public class ExtendsInterface {
public static void main(String[] args) {
Impl i = new Impl();
i.a();
i.b();
System.out.println(i instanceof A);
}
}a
b
true接口的声明
interface 定义一组能力契约。方法默认是 public abstract,字段默认是 public static final,都不需要(也不能)写这些修饰符。
interface Animal {
int MAX_AGE = 100; // public static final
void eat(); // public abstract
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("dog eats");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Animal a = new Dog();
a.eat();
System.out.println(Animal.MAX_AGE);
}
}dog eats
100接口的实现
类用 implements 实现接口,必须实现所有抽象方法,方法必须是 public(接口方法本来就是 public,实现时不能缩小权限)。
interface Flyer {
void fly();
}
interface Swimmer {
void swim();
}
class Duck implements Flyer, Swimmer {
@Override
public void fly() {
System.out.println("duck flies");
}
@Override
public void swim() {
System.out.println("duck swims");
}
}
public class MultiImpl {
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
Flyer f = d;
f.fly();
}
}duck flies
duck swims
duck fliesdefault 与 static 方法
Java 8 起接口可以有 default 方法(带实现,子类可直接用或覆盖)和 static 方法(只能通过接口名调用)。这让接口能平滑演进而不破坏已有实现。
interface Greeter {
void greet();
default void greetTwice() {
greet();
greet();
}
static Greeter of(String name) {
return () -> System.out.println("hi " + name);
}
}
public class DefaultDemo {
public static void main(String[] args) {
Greeter g = Greeter.of("Tom");
g.greetTwice();
}
}hi Tom
hi Tom接口的继承
接口之间也用 extends,而且可以一次继承多个。子接口继承父接口的抽象方法和 default 方法,实现类必须实现全部抽象方法。
interface A {
void a();
}
interface B extends A {
void b();
}
class Impl implements B {
@Override
public void a() {
System.out.println("a");
}
@Override
public void b() {
System.out.println("b");
}
}
public class ExtendsInterface {
public static void main(String[] args) {
Impl i = new Impl();
i.a();
i.b();
System.out.println(i instanceof A);
}
}a
b
true