接口和抽象类差不多,不过不是用class关键字表示的而是用interface关键字表示的
如下:
public abstract interface IContour {
}
接口像类一样也可以有父接口,并且可以有多个父接口
public abstract interface IContour[extends 接口1,接口2,..] {
}
类实现接口的语法为:
public class Roadster extends Car implements IContour{
}
在类中实现接口:
public abstract interface IContour {
public abstract void contour();
}
public abstract class Car {
public abstract void ride();
public abstract void speed();
}
public class Roadster extends Car implements IContour{
public void ride() {
System.out.println("Sports car can take people");
}
public void speed() {
System.out.println("Fast sport Car");
}
public void contour() {
System.out.println("Sports car is very cool");
}
}
Roadster类继承Car类并且在Roadster类实现了接口。
public static void main(String[] args) {
Car roadster = new Roadster();
roadster.ride();
roadster.speed();
IContour con = new Roadster();
con.contour();
}
在main()函数里实现了接口的对象。
result is:
Sports car can take people
Fast sport Car
Sports car is very cool