java接口对象的实例化2

2019年2月16日23:11:50 发表评论 1,714 views
import com.sun.management.UnixOperatingSystemMXBean;

/**
 * Created by xabcd on 2019/2/16.
 */
interface java_usb
{
    public void start();
    public void stop();
}
class MoveDisk implements java_usb
{
    public void start()
    {
        System.out.println("MoveDisk start...");
    }
    public void stop()
    {
        System.out.println("MoveDisk stop...");
    }
}
class Mp3 implements java_usb
{
    public void start()
    {
        System.out.println("Mp3 start...");
    }
    public void stop()
    {
        System.out.println("Mp3 stop...");
    }
}
class Computer
{
    public void work(java_usb u)
    {
        u.start();
        u.stop();
    }
}






/**
 * Created by xabcd on 2019/2/16.
 */
public class test_usb
{
    public static void main(String args[])
    {
        new Computer().work(new MoveDisk());
        new Computer().work(new Mp3());
    }
}

结果:
MoveDisk start...
MoveDisk stop...
Mp3 start...
Mp3 stop...

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: