OK的,樓主,我運(yùn)行過了,如下:
成都創(chuàng)新互聯(lián)電話聯(lián)系:18980820575,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)頁(yè)制作領(lǐng)域十余年,包括成都廣告制作等多個(gè)方面擁有多年的網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián),為網(wǎng)站錦上添花。
interface DriveControl {
void startEngine();
}
interface Repairable {
void repair();
}
public class Car implements DriveControl, Repairable {
String model;
public Car() {
System.out.println("Car init");
}
public Car(String model) {
this.model = model;
}
void printModel() {
System.out.println("The model of this car is" + this.model);
}
public void startEngine() {
System.out.println("Start engine");
}
public void repair() {
System.out.println("Car is repaired");
}
public static void main(String[] args) {
Car car = new Car();
DriveControl control = (DriveControl) car;
control.startEngine();
Repairable repairable = (Repairable) car;
repairable.repair();
}
}
運(yùn)行結(jié)果:
Car init
Start engine
Car is repaired
非常簡(jiǎn)單,基礎(chǔ)語(yǔ)法:
public interface 接口名 {
常量定義;
抽象方法定義;
default 方法定義;
}
即使你在接口中不定義任何方法,也是可以的。
Display.java ? 接口代碼如下:
public?interface?Display?{
public?void?dis();
}
接口的實(shí)現(xiàn)類DisplayImpl.java ? ?代碼如下:
public?class?DisplayImpl?implements?Display?{
@Override
public?void?dis()?{
//?TODO?Auto-generated?method?stub
System.out.println("輸出方法");
}
public?static?void?main(String[]?args)?{
new?DisplayImpl().dis();
}
}