电脑连接usb读取手机、U盘、硬盘的内容
interface USB {
String read();
void write(String s);
}
class Flash implements USB {
String id;
String memony;
public Flash(String id) {
this.id = id;
}
public String read() {
System.out.println("读取U盘的内容:");
System.out.println(memony);
return memony;
}
public void write(String s) {
memony = s;
System.out.println("向U盘中写操作成功!");
}
}
class HardDisk implements USB {
String id;
String memony = null;
public HardDisk(String s) {
id = s;
}
public String read() {
System.out.println("读取硬盘的内容:");
System.out.println(memony);
return memony;
}
public void write(String s) {
memony = s;
System.out.println("向硬盘中写操作成功!");
}
}
class Mobile implements USB {
String id;
String number;
String memony = null;
public Mobile(String id) {
this.id = id;
}
public String read() {
System.out.println("读取手机的内容:");
System.out.println(memony);
return memony;
}
public void write(String s) {
memony = s;
System.out.println("向手机存储中写操作成功!");
}
}
class Computer1 {
String logo;
USB USB;
String content;
public Computer1(String logo) {
this.logo = logo;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean insertUSB(USB u) {
boolean flag = false;
if (USB == null) {
USB = u;
flag = true;
System.out.println("USB设备连接成功!");
} else {
System.out.println("USB设备连接失败!");
}
return flag;
}
public boolean removeUSB(USB u) {
boolean flag = false;
if (USB != null) {
USB = null;
flag = true;
System.out.println("USB设备卸载成功!");
} else {
System.out.println("USB设备卸载失败!");
}
return flag;
}
public void readUSB(USB u) {
content = u.read();
}
public void writerUSB(USB u, String content) {
u.write(content);
}
}
public class TestUsb {
public static void main(String[] args) {
Computer1 computer = new Computer1("lenovo");
Mobile mobile = new Mobile("apple");
HardDisk hard_disk = new HardDisk("sumsung");
String content;
if (computer.insertUSB(mobile)) {
computer.readUSB(mobile);
System.out.println("从手机中读取的内容是:");
if (computer.getContent() == null) {
System.out.println("哈!是新手,没有内容!");
} else {
System.out.println(computer.getContent());
}
}
content = "这个手机是我的了!";
computer.writerUSB(mobile, content);
computer.readUSB(mobile);
if (computer.insertUSB(hard_disk)) {
content = "这个硬盘也是我的啦!!";
computer.writerUSB(hard_disk, content);
}
computer.removeUSB(hard_disk);
computer.readUSB(hard_disk);
}
}