java interface的一个经典实例

来源:互联网 发布:mysql c开发工具 编辑:程序博客网 时间:2024/04/25 15:49

本文转自 http://blog.chinaunix.net/u/22381/showart_251790.html

 

import java.io.*;
interface CAR
{
    void start();
    void stop();
}
class SmallCar implements CAR
{
    public void start()
    {
        System.out.println("smallcar start...");
    }
    public void stop()
    {
        System.out.println("smallcar stop!");
    }
}
class BigCar implements CAR
{
    public void start()
    {
        System.out.println("bigcar start...");
    }
    public void stop()
    {
        System.out.println("bigcar  stop!");
    }
}
class TestCar
{
    public void operCar(CAR c)
    {
        c.start();
        c.stop();
    }
}
public class TestInterface
{
    public static void main(String[] args)
    {
         TestCar tc=new TestCar();
         SmallCar sc=new SmallCar();
         BigCar  bc = new BigCar();
         tc.operCar(sc);
         tc.operCar(bc);
    }
}

 

发表于: 2007-03-01,修改于: 2007-03-01 16:51,已浏览3410次,有评论5条 推荐 投诉 网友: 本站网友 时间:2008-09-01 11:16:04 IP地址:210.76.124.★  
class TestCar
{
    public void operCar(CAR c)
    {
        c.start();
        c.stop();
    }
}

原创粉丝点击