Created
September 19, 2015 22:02
-
-
Save Pornpun/9713fae10e9556f3ce49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mystou; | |
public class Canon implements PrinterInterface { | |
@Override | |
public void on(Object object) { | |
// code... | |
System.out.println("เปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public void off(Object object) { | |
// code... | |
System.out.println("ปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public String print(Object object, String name) { | |
// code... | |
boolean result = true; // สมสมติกรณีพิมพ์สำเร็จ | |
String msg = "พิมพ์ไฟล์"; | |
if(result) { | |
msg += name + " สำเร็จ " + object.getClass().getName(); | |
} else { | |
msg += name + " ล้มเหลว " + object.getClass().getName(); | |
} | |
return msg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mystou; | |
public class Epson implements PrinterInterface { | |
@Override | |
public void on(Object object) { | |
// code... | |
System.out.println("เปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public void off(Object object) { | |
// code... | |
System.out.println("ปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public String print(Object object, String name) { | |
// code... | |
boolean result = true; // สมสมติกรณีพิมพ์สำเร็จ | |
String msg = "พิมพ์ไฟล์"; | |
if(result) { | |
msg += name + " สำเร็จ " + object.getClass().getName(); | |
} else { | |
msg += name + " ล้มเหลว " + object.getClass().getName(); | |
} | |
return msg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mystou; | |
public class HP implements PrinterInterface { | |
@Override | |
public void on(Object object) { | |
// code... | |
System.out.println("เปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public void off(Object object) { | |
// code... | |
System.out.println("ปิดเครื่อง " + object.getClass().getName()); | |
} | |
@Override | |
public String print(Object object, String name) { | |
// code... | |
boolean result = true; // สมสมติกรณีพิมพ์สำเร็จ | |
String msg = "พิมพ์ไฟล์"; | |
if(result) { | |
msg += name + " สำเร็จ " + object.getClass().getName(); | |
} else { | |
msg += name + " ล้มเหลว " + object.getClass().getName(); | |
} | |
return msg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mystou; | |
public class InterfaceTest { | |
public static void main(String[] args) { | |
//การทำงานของเครื่องพิมพ์ Canon | |
Canon canon = new Canon(); | |
canon.on(canon); | |
System.out.println(canon.print(canon,"OOP")); | |
canon.off(canon); | |
System.out.println("===============================\n\n"); | |
// การทำงานของเครื่องพิมพ์ Epson | |
Epson epson = new Epson(); | |
epson.on(epson); | |
System.out.println(epson.print(epson,"หนังสือภาษาไทย")); | |
epson.off(epson); | |
System.out.println("===============================\n\n"); | |
// การทำงานของเครื่องพิมพ์ Canon | |
HP hp = new HP(); | |
canon.on(hp); | |
System.out.println(hp.print(hp,"Java Programming")); | |
canon.off(hp); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mystou; | |
public interface PrinterInterface { | |
public void on(Object object); // เปิดเครื่อง | |
public void off(Object object); // ปิดเครื่อง | |
public String print(Object object, String name); // สั่งพิมพ์ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment