Head First Java 泳池迷宫参考解答————8 深入多态

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()方法。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值