package ldb0708.sh.demo05;
public interface USB {
//返回一个数字
int getInt();
}
package ldb0708.sh.demo05;
public class UsbDriver implements USB {
@Override
public int getInt() {
// TODO Auto-generated method stub
return 1;
}
}
package ldb0708.sh.demo05;
public class UsbKeyBoard implements USB {
@Override
public int getInt() {
// TODO Auto-generated method stub
return 2;
}
}
package ldb0708.sh.demo05;
public class UsbLight implements USB {
@Override
public int getInt() {
// TODO Auto-generated method stub
return 3;
}
}
package ldb0708.sh.demo05;
public class Computer {
//设置接口
private USB usb;
//定义方法
public void print() {
if(usb!=null) { //调用接口USB中的方法
System.out.println(usb.getInt());
}else {
System.out.println("没有数字");
}
}
public USB getUsb() {
return usb;
}
public void setUsb(USB usb) {
this.usb = usb;
}
}
11-02