package zhang.javase.TestInterface;
/**
- 接口的运用
*接口可以理解成usb接口,而手机,打印机可以看成接口的实现类 - @作者:小章鱼
/
public class TestUsb {
public static void main(String[] args) {
computer com = new computer();
com.dowork(new pinter());
System.out.println("--------------");
Flash f = new Flash();
com.dowork(f);
System.out.println("--------------");
// 接口实现匿名对象的实现(方法一)
USB phone = new USB() {
@Override
public void start() {
System.out.println(“phone is working”);
}
@Override
public void end() {
System.out.println(“phone is working over”);
}
};
com.dowork(phone);
System.out.println("--------------------");
// 接口实现匿名对象的实现
com.dowork(new USB() {
@Override
public void start() {
System.out.println(“mac is working”);
}
@Override
public void end() {
System.out.println(“mac is working over”);
}
});
}
}
/* - @作者:小章鱼
*电脑类
*/
class computer {
//此处体现了接口的多态性
public void dowork(USB mydevice) {
System.out.println(“设备已被连接”);
mydevice.start();
System.out.println("please let size equals " + USB.size);
mydevice.end();
System.out.println(“已将你的设备安全退出”);
}
}
interface USB {
final double size = 12.5;
void start();
void end();
}
class pinter implements USB {
@Override
public void start() {
System.out.println(“printer is working”);
}
@Override
public void end() {
System.out.println("printer is working over");
}
}
class Flash implements USB {
@Override
public void start() {
System.out.println(“flash is working”);
}
@Override
public void end() {
System.out.println("flash is working over");
}
}