Lesson_8 课堂练习 ---- 继承.通讯工具
练习描述:
Mobile 类和 FixTelephone 类继承于Communication类父类有显示本机号码,打电话,挂断的功能.子类模拟手机发短信,传真发
传真等功能
1 /*********************************************************** 2 * Lesson_8 -- 课堂练习 3 * 4 * 通讯工具 5 * 13-01-18 6 * by CocoonFan 7 ************************************************************ 8 ************************************************************ 9 * Mobile 类和 FixTelephone 类继承于Communication类 10 * 父类有显示本机号码,打电话,挂断的功能.子类模拟手机发短信,传真发 11 * 传真等功能 12 ************************************************************/ 13 14 public class TestCommunication { 15 public static void main(String[] args) { 16 Mobile mobile = new Mobile("123456"); 17 mobile.calling("8888"); 18 mobile.offset(); 19 mobile.showSelfNumber(); 20 mobile.sendMessage("我正在发短信..."); 21 22 FixTelephone fixTelephone = new FixTelephone("111111"); 23 24 fixTelephone.setFax("我正在发传真..."); 25 fixTelephone.setSelfNumber("6666"); 26 fixTelephone.showSelfNumber(); 27 fixTelephone.sendFax(); 28 } 29 } 30 31 class Communication{ 32 private String selfNumber; 33 private String toNumber; 34 35 public Communication(String selfNumber){ 36 this.selfNumber = selfNumber; 37 } 38 public String getToNumber() { 39 return toNumber; 40 } 41 42 public void setToNumber(String toNumber) { 43 this.toNumber = toNumber; 44 } 45 46 public String getSelfNumber() { 47 return selfNumber; 48 } 49 50 public void setSelfNumber(String selfNumber) { 51 this.selfNumber = selfNumber; 52 } 53 54 public void calling(String number){ 55 this.toNumber = number; 56 System.out.println("正在拨号:" + toNumber); 57 } 58 59 public void offset() { 60 System.out.println("已挂断!"); 61 } 62 63 public void showSelfNumber(){ 64 System.out.println("本机号码:" + selfNumber); 65 } 66 } 67 68 class Mobile extends Communication{ 69 70 public Mobile(String selfNumber) { 71 super(selfNumber); 72 } 73 public void sendMessage(String message){ 74 System.out.println(super.getSelfNumber() + "发送短信 给" 75 + super.getToNumber() + ": "+ message); 76 } 77 } 78 79 class FixTelephone extends Communication{ 80 private String fax; 81 82 public FixTelephone(String selfNumber){ 83 super(selfNumber); 84 } 85 public String getFax() { 86 return fax; 87 } 88 89 public void setFax(String fax) { 90 this.fax = fax; 91 } 92 93 public void sendFax(){ 94 System.out.println("传真内容:" + fax); 95 } 96 }