所谓的接口呢,其实就是规则,基本语法为: interface 接口名称 { 规则属性,规则的行为 }
接口其实都是抽象的,规则的属性必须为固定的,而且不能够修改。
属性和行为的访问权限必须是public.
属性应该是静态的,行为应该是抽象的。
接口和类是两个层面的东西,接口可以继承其他的接口,类的对象需要遵循接口,在java中
,这个遵循,称之为实现(implements),类需要实现接口,而且可以实现多个接口。
public class InterfaceTest {
public static void main(String[] args){
Computer c = new Computer();
Light light1 = new Light();
c.usb1 = light1;
Light light2 = new Light();
c.usb2=light2;
c.USBSupply();
}
}
interface USBInterface{
}
interface USBSupply extends USBInterface{
public void USBSupply();
}
interface USBReceived extends USBInterface{
public void USBReceived();
}
class Computer implements USBSupply{
USBReceived usb1;
USBReceived usb2;
public void USBSupply() {
System.out.println("computer supply power...");
usb1.USBReceived();
usb2.USBReceived();
}
}
class Light implements USBReceived{
public void USBReceived() {
System.out.println("Light received power...");
}
}