![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
import java.io.*;
interface CAR
{
void start();
void stop();
}
class SmallCar implements CAR
{
public void start()
{
System.out.println("smallcar start
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/2fb7a7a1351bea3c046ebb445cae4386.gif)
}
public void stop()
{
System.out.println("smallcar stop!");
}
}
class BigCar implements CAR
{
public void start()
{
System.out.println("bigcar start
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/2fb7a7a1351bea3c046ebb445cae4386.gif)
}
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);
}
}
类似C++中的纯虚函数,接口就是给出一些空的方法,到具体用的时候再由使用该接口的方法自己定义内容
要注意的是,想用接口必须实现接口的所有方法.