public interface FirstGeneration {
public void call(); //打电话
public void message(); //发短信
}
public interface SecondGeneration {
public void photo(); //拍照
public void playMusic(); //听音乐
}
public interface ThirdGeneration {
public void playVideo(); //播放视频
public void Internet(); //上网
}
public class Mobile implements FirstGeneration,SecondGeneration,ThirdGeneration{
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("打电话");
}
@Override
public void message() {
// TODO Auto-generated method stub
System.out.println("发短信");
}
@Override
public void photo() {
// TODO Auto-generated method stub
System.out.println("拍照");
}
@Override
public void playMusic() {
// TODO Auto-generated method stub
System.out.println("听音乐");
}
@Override
public void playVideo() {
// TODO Auto-generated method stub
System.out.println("播放视频");
}
@Override
public void Internet() {
// TODO Auto-generated method stub
System.out.println("上网");
}
}
public class Test {
public static void main(String[] args) {
Mobile mobile = new Mobile();
mobile.call();
mobile.message();
mobile.photo();
mobile.playMusic();
mobile.playVideo();
mobile.Internet();
}
}
组装电脑:
电脑的主要组成包括CPU,硬盘,内存,现在用程序模拟组装一台电脑的过程。
public interface CPU {
public void show();
}
public class CPUImpl implements CPU{
private double price;
private String speed;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public CPUImpl(double price, String speed) {
super();
this.price = price;
this.speed = speed;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("CPU信息【价格:"+this.price+",主频:"+this.speed+"】");
}
}
public class NewCPUImpl implements CPU{
private double price;
private String speed;
private String thirdCache;
public NewCPUImpl(double price, String speed, String thirdCache) {
super();
this.price = price;
this.speed = speed;
this.thirdCache = thirdCache;
}
public void show() {
System.out.println("CPU信息【价格:"+this.price+",主频:"+this.speed+",三级缓存:"+this.thirdCache+"】");
}
}
public interface HardDisk {
public void show();
}
public class HardDiskImpl implements HardDisk{
private double price;
private String size;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public HardDiskImpl(double price, String size) {
super();
this.price = price;
this.size = size;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("硬盘信息【价格:"+this.price+",容量:"+this.size+"】");
}
}
public interface Memory {
public void show();
}
public class MemoryImpl implements Memory{
private double price;
private String size;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public MemoryImpl(double price, String size) {
super();
this.price = price;
this.size = size;
}
@Override
public void show() {
System.out.println("内存信息【价格:"+this.price+",容量:"+this.size+"】");
}
}
public class Computer {
private CPU cpu;
private HardDisk hardDisk;
private Memory memory;
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public HardDisk getHardDisk() {
return hardDisk;
}
public void setHardDisk(HardDisk hardDisk) {
this.hardDisk = hardDisk;
}
public Memory getMemory() {
return memory;
}
public void setMemory(Memory memory) {
this.memory = memory;
}
public Computer(CPU cpu, HardDisk hardDisk, Memory memory) {
super();
this.cpu = cpu;
this.hardDisk = hardDisk;
this.memory = memory;
}
public void show() {
System.out.println("电脑配置如下:");
this.cpu.show();
this.hardDisk.show();
this.memory.show();
}
}
public class Test {
public static void main(String[] args) {
//创建零件
CPU cpu = new CPUImpl(2600, "3.7GHz");
// CPU cpu = new NewCPUImpl(3000, "3.7GHz", "12MB");
HardDisk hardDisk = new HardDiskImpl(299,"1TB");
Memory memory = new MemoryImpl(699, "8GB");
//创建电脑,组装零件
Computer computer = new Computer(cpu, hardDisk, memory);
computer.show();
}
}