package com.whxiong.work05;
import java.util.Scanner;
import com.whxiong.work05_2.Friends;
import com.whxiong.work05_2.Master;/**
* 第五章课后习题测试类 * @param args */public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in);System.out.println("课后习题第五题");
System.out.println("************************************"); /** * 实例化ChoosePrinter */ ChoosePrinter printer01 = new ChoosePrinter(); /** * 选择打印机类型 */ System.out.println("请选择您要使用的打印机:1.针式打印机 2.喷墨打印机 3.激光打印机"); int typeid = input.nextInt(); Printer printer = printer01.gettype(typeid); if (printer != null) { printer01.print(printer); } else { System.out.println("对不起,没有该类型打印机!"); } System.out.println(); System.out.println("课后习题第六题"); System.out.println("************************************"); Master master=new Master("牛犇"); System.out.println("请选择牛犇朋友的国家:1.中国 2.美国"); int chooseCountry=input.nextInt(); Friends friends=master.Treat(chooseCountry); if(friends!=null){ System.out.println("谢谢"+master.getName()+"的热情招待!"); }else{ System.out.println("不好意思,牛犇不认识您!"); }********************************************************
package com.whxiong.work05;
/** * Frinends 牛犇的朋友类 * @author whx */public abstract class Friends { private String country; //国家 private String name; //名字 private String eat; //食物 private String play; //运动 /** * 带参构造 * @param country * @param name * @param eat * @param play */ public Friends(String country,String name, String eat, String play) { super(); this.country=country; this.name = name; this.eat = eat; this.play = play; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEat() { return eat; } public void setEat(String eat) { this.eat = eat; } public String getPlay() { return play; } public void setPlay(String play) { this.play = play; } /** * 定义抽象类的Eat方法 */ public abstract void Eat(); /** * 定义抽象类的Play方法 */ public abstract void Play();}
/** * America 美国朋友 子类 * @author whx */ class America extends Friends { /** * 调用父类中的带参构造 * @param country * @param name * @param eat * @param play */ public America(String country,String name, String eat, String play) { super(country,name, eat, play); }@Override
public void Eat() { { System.out.println("我叫"+this.getName()+",我是"+this.getCountry() +"人"+"\n\t我喜欢吃"+this.getEat()); } }@Override
public void Play() { System.out.println("\t我喜欢的运动是"+this.getPlay()); }} /** * China 中国朋友 子类 * @author whx */ class China extends Friends{ /** * 调用父类中的带参构造 * @param country * @param name * @param eat * @param play */ public China(String country,String name, String eat, String play) { super(country,name, eat, play); }@Override
public void Eat() { System.out.println("我叫"+this.getName()+",我是" +this.getCountry()+"人"+"\n\t我喜欢吃"+this.getEat()); }@Override
public void Play() { System.out.println("\t我喜欢的运动是"+this.getPlay()); } }/**
* Master 主人牛犇 类 * @author whx */ class Master { private String name;//主人姓名 /** * 带参构造 * @param name */ public Master(String name){ this.name=name; } public String getName() { return name; }public void setName(String name) {
this.name = name; }/**
* @param chooseCountry选择国家 * @return */ public Friends Treat(int chooseCountry){ Friends friends=null; if(chooseCountry==1){ friends=new China("中国","王小强", "四川菜", "太极拳"); friends.Eat(); friends.Play(); } if(chooseCountry==2){ friends=new America("美国","约翰", "比萨", "橄榄球"); friends.Eat(); friends.Play(); } return friends; }*************************************************package com.whxiong.work05;
/** * Printer 创建打印机类 * abstract 抽象不可被实例化 * @author whx */public abstract class Printer { //打印机类型 private String type; /** * 带参构造 * @param type */ public Printer(String type){ this.type=type; } public String getType() { return type; }public void setType(String type) {
this.type = type; }/**
* 定义抽象方法print */ public abstract void print();} /** * DotMatrixPrinter 针式打印机 子类 * @author whx */ class DotMatrixPrinter extends Printer { public DotMatrixPrinter(String type) { super(type); } @Override public void print() { System.out.println("***您选择的打印机是" + this.getType() + "***"); } } /** * InkpetPrinter 喷墨打印机 子类 * * @author whx */ class InkpetPrinter extends Printer { public InkpetPrinter(String type) { super(type); } @Override public void print() { System.out.println("***您选择的打印机是" + this.getType() + "***"); } } /** * LaserPrinter 激光打印机 子类 * @author whx */ class LaserPrinter extends Printer { public LaserPrinter(String type) { super(type); } @Override public void print() { System.out.println("***您选择的打印机是" + this.getType() + "***"); } } /** * ChoosePrinter 选择打印机 * @author whx */ class ChoosePrinter { /** * 选择打印机类型 * @param id打印机编号 */ public Printer gettype(int typeid) { Printer printer = null; if (typeid == 1) { printer = new DotMatrixPrinter("针式打印机"); } if (typeid == 2) { printer = new DotMatrixPrinter("喷墨打印机"); } if (typeid == 3) { printer = new DotMatrixPrinter("激光打印机"); } return printer; }/**
* 打印机开始打印 * @param printer */ public void print(Printer printer) { /** * 判断对象printer是InkpetPrinter类型 */ if (printer instanceof InkpetPrinter) { /** * 由于printer是父类对象,强制转换 */ InkpetPrinter ink = (InkpetPrinter) printer; ink.print(); } /** * 判断对象printer是LaserPrinter类型 */ if (printer instanceof LaserPrinter) { /** * 由于printer是父类对象,强制转换 */ LaserPrinter laser = (LaserPrinter) printer; laser.print(); } /** * 判断对象printer是DotMatrixPrinter类型 */ if (printer instanceof DotMatrixPrinter) { /** * 由于printer是父类对象,强制转换 */ DotMatrixPrinter dot = (DotMatrixPrinter) printer; dot.print(); } }