面向对象 第5章

课后练习5:
package five_c5;

public abstract class Printer {
	public abstract void print();
}

package five_c5;

public class LaserPrinter extends Printer{
	public void print() {
		System.out.println("激光打印");
	}
	public void test(Printer test) {
		System.out.println("哈哈哈哈");
	}
}

package five_c5;

public class InkpetPrinter extends Printer {
	public void print() {
		System.out.println("喷墨打印");
	}
}

package five_c5;

public class DotMatrixPrinter extends Printer{
	public void print() {
		System.out.println("针式打印");
	}
}

package five_c5;

public class Test {
	public static void main(String[] args) {
		//Printer printer = new DotMatrixPrinter();
		//Printer printer = new InkperPrinter();
		Printer printer = new LaserPrinter();
		printer.print();
		Printer test = new LaserPrinter();
		if(test instanceof LaserPrinter) {
			LaserPrinter p = (LaserPrinter)test;
			p.test(test);
		}
	}
}

课后练习6:
package five_c6;

public abstract class Niu {
	public abstract void eat();
}

package five_c6;

public class Wang extends Niu {
	public void eat() {
		System.out.println("王小强喜欢吃四川菜");
	}
	public void riot() {
		System.out.println("王小强练太极拳");
	}
}

package five_c6;

public class John extends Niu {
	public void eat() {
		System.out.println("约翰喜欢吃披萨");
	}
	public void ball() {
		System.out.println("约翰打橄榄球");
	}
}

package five_c6;

public class Test {
	public static void main(String[] args) {
		Niu niu = new Wang();
		niu.eat();
		if(niu instanceof John) {
			John john = (John)niu;
			john.ball();
		}else if(niu instanceof Wang) {
			Wang wang = (Wang)niu;
			wang.riot();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值