接口的应用

public class TestUSB {
public static void main(String[] args) {
computer m=new computer();
m.dowork(new Flash());

phone p=new phone();
m.dowork(p);

//实现接口的匿名类对象
m.dowork(new USB() {
public void start(){
System.out.println("匿名类开始工作");
}
public void stop()
{
System.out.println("匿名类结束工作");
}

}
);
}
}

interface USB
{
//尺寸为常量

//功能为抽象方法
void start();
void stop();
}
class computer
{
public void dowork(USB usb){
usb.start();
System.out.println("此设备开始操作");
usb.stop();
}

}

class Flash implements USB
{
public void start()
{
System.out.println("U盘开始工作");
}
public void stop()
{
System.out.println("u盘停止工作");
}

}

class phone implements USB
{
public void start()
{
System.out.println("手机开始工作");
}
public void stop()
{
System.out.println("手机停止工作");
}

}

原文地址:https://www.cnblogs.com/steel-chen/p/6843484.html