interface Nose { //新建接口
public int iMethod();
//接口中的方法
}
abstract class Picasso implements Nose { //抽象类Picasso
public int iMethod() {
//重写方法
return 7;//返回值 7
}
}
class Clowns extends Picasso {}; //子类Clowns继承Picasso
class Acts extends Picasso { //子类Acts继承Picasso
public int iMethod() {
//重写方法
return 5;//返回值 5
}
}
public class Of76 extends Clowns{ //子类Of76继承Clowns
public static void main (String[] args) {
//主方法
Nose [] i = new Nose[3]; //创建三个对象
i[0] = new Acts();
i[1] = new Clowns();
i[2] = new Of76();
for (int x = 0; x < 3; x ++) {
System.out.println(i[x].iMethod() + " " + i[x].getClass());
}
}
}
//运行结果:
5 class Acts
7 class Clowns
7 class Of76
三个对象分别调用iMethod()方法,Acts()重写方法之后返回值为5。
Clowns()中没有iMethod()方法,由于继承的Picasso(),在Picasso()类中的iMethod()方法返回值为7。
Of76()也没有iMethod()方法,继承自Clowns(),所以也调用的Picasso()的iMethod()方法。