课后练习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();
}
}
}
面向对象 第5章
最新推荐文章于 2022-03-07 13:18:11 发布