1 interface Playable {
2 void play();
3 }
4 interface Bounceable {
5 void play();
6 }
7 interface Rollable extends Playable, Bounceable {
8 Ball ball = new Ball(“PingPang”);
9 }
10 class Ball implements Rollable {
11 private String name;
12 public String getName() {
13 return name;
14 }
15 public Ball(String name) {
16 this.name = name;
17 }
18 public void play() {
19 ball = new Ball(“Football”);
20 System.out.println(ball.getName());
21 }
22 }
错。"interface Rollable extends Playable, Bounceable"没有问题。interface可继承多个interfaces,所以这里没错。问题出在interface Rollable里的"Ball ball = new Ball("PingPang");"。任何在interface里声明的interface variable (接口变量,也可称成员变量),默认为public static final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new Ball("PingPang");"。在Ball类的Play()方法中,"ball = new Ball("Football");"改变了ball的reference,而这里的ball来自Rollable interface,Rollable interface里的ball是public static final的,final的object是不能被改变reference的。因此编译器将在"ball = new Ball("Football");"这里显示有错。