Define mouse , keyboard , The rules of notebook
interface USB { void open();// Open function void close();// Turn off the function }
Mouse implementation USB rule
class Mouse implements USB { public void open() { System.out.println(" Mouse on ");
} public void close() { System.out.println(" Mouse off "); } }
Keyboard implementation USB rule
class KeyBoard implements USB { public void open() {
System.out.println(" Keyboard on "); } public void close() { System.out.println(" Keyboard off ");
} }
Define notebook
class NoteBook { // Notebook open operation function public void run() { System.out.println(" Notebook running ");
} // Notebook use usb equipment , When the notebook object calls this function , It must be passed a match USB Regular USB equipment public void useUSB(USB
usb) { // Judge whether there is USB equipment if (usb != null) { usb.open(); usb.close(); } } public
void shutDown() { System.out.println(" Notebook off "); } }
public class Test { public static void main(String[] args) { // Create notebook entity object
NoteBook nb = new NoteBook(); // Notebook on nb.run(); // Create a mouse entity object Mouse m = new
Mouse(); // Notebook using mouse nb.useUSB(m); // Create keyboard entity object KeyBoard kb = new KeyBoard(); //
Notebook using keyboard nb.useUSB(kb); // Notebook off nb.shutDown(); } }
Technology