java源文件分享地址:
链接:https://pan.baidu.com/s/1ydG2Vv9TmXKXqaD-hLw2PA
提取码:xwtc
复制这段内容后打开百度网盘手机App,操作更方便哦
作业一、接口实现手机
原始的手机,可以发短信,通电话。随着发展,手机增加了功能:音频、视频播放、拍照、上网。使用接口实现手机功能;
首先创建手机父类:
package com.phone.jiekou; public abstract class HandSet { private String brand; private String type; public HandSet() {} public HandSet(String brand,String type) { this.brand = brand; this.type = type; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getType() { return type; } public void setType(String type) { this.type = type; } public abstract void message(); public abstract void call(); }
创建手机功能接口:
1、上网功能接口:
package com.phone.jiekou; public interface Network { public void net(); }
2、拍照功能接口:
package com.phone.jiekou; public interface Photo { public void takePhoto(); }
3、播放视频功能接口:
package com.phone.jiekou; public interface Play { public void play(String film); }
4、播放音频功能接口:
package com.phone.jiekou; public interface Music { public void boFang(String music); }
创建普通手机类要求拥有播放音频功能:
package com.phone.jiekou; public class CommonPhone extends HandSet implements Music{ public CommonPhone() { } public CommonPhone(String brand,String type) { super(brand,type); } public void showInfo() { System.out.println("手机信息及功能介绍:"); System.out.println("这是一部"+this.getBrand()+"的手机,型号是"+this.getType()); } public void message() { System.out.println("发送短信!"); } public void call() { System.out.println("拨打电话!"); } public void boFang(String music) { System.out.println("可以播放"+music+"!"); } }
创建智能手机类要求拥有上网功能、拍照功能、播放视频功能
package com.phone.jiekou; public class SmartPhone extends HandSet implements Network, Photo, Play { public SmartPhone() {} public SmartPhone(String brand,String type) { super(brand,type); } public void showInfo() { System.out.println("手机信息及功能介绍:"); System.out.println("这是一部"+this.getBrand()+"的手机,型号是"+this.getType()); } public void play(String film) { System.out.println("播放电影"+film); } public void takePhoto() { System.out.println("拍照功能"); } public void net() { System.out.println("上网功能"); } public void message() { System.out.println("发送短信"); } public void call() { System.out.println("拨打电话"); } }
编写测试类:
package com.phone.jiekou; public class TestPhone { public static void main(String[] args) { CommonPhone cphone = new CommonPhone("三星","I3452"); cphone.showInfo(); cphone.message(); cphone.call(); cphone.boFang("音乐"); SmartPhone sphone = new SmartPhone("苹果","6P"); sphone.showInfo(); sphone.message(); sphone.call(); sphone.play("大话西游"); sphone.net(); sphone.takePhoto(); } }
实现结果为:
手机信息及功能介绍:
这是一部三星的手机,型号是I3452
发送短信!
拨打电话!
可以播放音乐!
手机信息及功能介绍:
这是一部苹果的手机,型号是6P
发送短信
拨打电话
播放电影大话西游
上网功能
拍照功能
作业二、组装一台计算机
1、采用面向接口编程思想组装一台计算机
2、计算机的主要组成部分:cpu、硬盘、内存
实现思路:
定义CPU的接口cpu,返回CPU品牌和主频;
定义内存的接口EMS,返回类型和容量;
定义硬盘的接口HardDisk,返回容量;
编写各组件厂商分别实现CPU、EMS、和HardDisk接口
编写计算机类,组装计算机并显示相关信息
编写测试类运行
1、编写cpu功能接口
package com.cpu; public interface Cpu { public String brand(); public String hz(); }
1、1 生产AmdCpu
package com.cpu; public class AmdCpu implements Cpu { public String brand() { return "Amd"; } public String hz() { return "2.8G"; } }
1、2 生产IntelCpu
package com.cpu; public class IntelCpu implements Cpu { public String brand() { return "Intel"; } public String hz() { return "3.0G"; } }
2、编写内存功能接口
package com.EMS; public interface Ems { public String type(); public String room(); }
2、1 生产Jinstom品牌的内存
package com.EMS; public class Jinstom implements Ems { public String type() { return "DDR3"; } public String room() { return "4G"; } }
2、2 生产SunXing品牌的内存
package com.EMS; public class SunXing implements Ems { public String type() { return "DDR4"; } public String room() { return "5G"; } }
3、编写硬盘功能的接口
package com.HardDisk; public interface HardDisk { public String hdd(); }
3、1 生产HaiLishi品牌的硬盘
package com.HardDisk; public class HaiLishi implements HardDisk { public String hdd() { return "500G"; } }
3、2 生产XiShu品牌的硬盘
package com.HardDisk; public class XiShu implements HardDisk { public String hdd() { return "1T"; } }
创建电脑组装类
package com.computer; import com.EMS.Ems; import com.HardDisk.HardDisk; import com.cpu.Cpu; public class Computer { private Cpu cpu; private Ems ems; private HardDisk hdd; public Cpu getCpu() { return cpu; } public void setCpu(Cpu cpu) { this.cpu = cpu; } public Ems getEms() { return ems; } public void setEms(Ems ems) { this.ems = ems; } public HardDisk getHdd() { return hdd; } public void setHdd(HardDisk hdd) { this.hdd = hdd; } public void showInfo() { System.out.println("计算机配置信息:"); System.out.println("CPU是:"+cpu.brand()+"品牌,主频是:"+cpu.hz()+"Hz;"); System.out.println("内存类型是:"+ems.type()+",容量为:"+ems.room()+";"); System.out.println("硬盘容量为:"+hdd.hdd()+";"); } }
编写测试类:
package com.user; import com.EMS.Ems; import com.EMS.Jinstom; import com.HardDisk.HaiLishi; import com.HardDisk.HardDisk; import com.computer.Computer; import com.cpu.Cpu; import com.cpu.IntelCpu; public class ComputerUser { public static void main(String[] args) { Computer com= new Computer(); Cpu cpu = new IntelCpu(); Ems ems = new Jinstom(); HardDisk hdd = new HaiLishi(); com.setCpu(cpu); com.setEms(ems); com.setHdd(hdd); com.showInfo(); } }
实现结果为:
计算机配置信息: CPU是:Intel品牌,主频是:3.0GHz; 内存类型是:DDR3,容量为:4G; 硬盘容量为:500G;