1 / 27

ครั้งที่ 10 Interface

ครั้งที่ 10 Interface. มูลเหตุจูงใจ. จาวาได้เสนอกลไกการสืบทอดแบบพิเศษ นอกเหนือจากการ extends ปกติ เพื่อแก้ปัญหา กรณีที่คลาสลูกต้องการมีคลาสแม่มากกว่า 1 คลาส ถ้าเป็นการ inherit ตามปกติ คลาสลูกจะสามารถ extends คลาสแม่ ได้สูงสุดเพียงคลาสเดียวเท่านั้น

prisca
Download Presentation

ครั้งที่ 10 Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ครั้งที่ 10Interface

  2. มูลเหตุจูงใจ • จาวาได้เสนอกลไกการสืบทอดแบบพิเศษ นอกเหนือจากการ extends ปกติ • เพื่อแก้ปัญหา กรณีที่คลาสลูกต้องการมีคลาสแม่มากกว่า 1 คลาส • ถ้าเป็นการ inherit ตามปกติ คลาสลูกจะสามารถ extends คลาสแม่ • ได้สูงสุดเพียงคลาสเดียวเท่านั้น • แต่ถ้าคลาสแม่เป็น interface คลาสลูกจะสามารถสืบทอดคลาสแม่ได้มากกว่า 1 คลาส

  3. Interface in Java • เป็นคลาสที่มีกฎเกณฑ์พิเศษ ดังนี้ • ประกาศด้วยคำว่า interface แทนคำว่า class และไม่สามารถนำไปสร้าง object ได้ • attribute และ method ภายใน interface ต้องประกาศเป็น public หรือ default เท่านั้น • ไม่มี constructor • ทุก method จะต้องเป็น abstract method

  4. ตัวอย่างการเขียน interface (1)

  5. ตัวอย่างการเขียน interface (2)

  6. ประโยชน์ของ interface • ใช้ในการสร้างความสัมพันธ์ inheritance แบบซับซ้อน • จาวามักใช้กลไกนี้ร่วมกับ abstract class • เพื่อกำหนดมาตรฐานการใช้งาน code โปรแกรมที่ไม่ขึ้นกับบริษัทหรือผู้ผลิตแต่ละยี่ห้อ • จาวากำหนด interface เอาไว้ แล้วเปิดให้บริษัทต่างๆ นำ interface นี้ไปสืบทอด เพื่อพัฒนาโปรแกรมด้านต่าง ๆ • ทำให้ผู้ใช้สามารถเลือกใช้ SW ของผู้ผลิตใดก็ได้ โดยที่การใช้งานยังเหมือนมาตรฐานที่จาวากำหนดไว้

  7. interface • ถูกสร้างขึ้นเพื่อกำหนดโครงสร้างของ method ที่จำเป็นต้องใช้งานขึ้นมา • แต่ยังไม่กำหนดรายละเอียดการทำงานใดๆให้กับ method • ทุก method ใน interface จึงเป็น abstract method • รายละเอียดของ method เหล่านี้จะถูก implement โดยคลาสที่เรียกใช้งาน interface นี้

  8. ลักษณะของ interface ใช้คำนี้แทนคำว่า class public interface Entertainer { public abstract void sing(); public abstract void dance(); } • การประกาศ interface ทำได้โดยระบุ keywork interface ดังตัวอย่าง • ภายใน interface มี 2 method ซึ่งเป็น abstract method ทั้งหมด • เป็น method ว่างเปล่ายังไม่มีการเขียนรายละเอียดการทำงาน ทุก method ต้องเป็น abstract method

  9. ลักษณะของ interface • นอกเหนือจากการสร้าง method เรายังสามารถประกาศ attribute ขึ้นภายใน interface ได้ด้วย • โดยมีข้อจำกัด ดังนี้ • attribute ทุกตัวจะต้องถูกประกาศเป็น public static final เท่านั้น • ตัวแปรใน interface จะถูกกำหนดค่าคงที่ตั้งแต่ตอนต้น และจะมีค่าเช่นนี้ไปตลอดการทำงานของโปรแกรม *** แม้เราจะประกาศโดยไม่ระบุให้เป็น final มันก็จะเป็นไปตามอัตโนมัติ ดังนั้น ข้อแนะนำก็คือ ประกาศไปเลยดีกว่า เวลาใช้เราจะได้ไม่ลืม ****

  10. ตัวอย่าง การประกาศตัวแปรใน interface public interface Entertainer { public static final String PRINCIPLE = “The show must go on”; //ควรจะเขียนให้เต็มรูปแบบ String PRINCIPLE = “The show must go on”; //จะเขียนแค่นี้ก็ได้ แต่จะมี modifier เหมือนคำสั่งข้างบนโดยอัตโนมัติ }

  11. การใช้งาน interface • คลาสใดต้องการใช้งาน interface จะต้องระบุ keyword implements ตามด้วยชื่อ interface ที่ต้องการใช้งาน • ตัวอย่าง • public class Singer implements Entertainer { } หมายถึง คลาส Singer ต้องการใช้งาน interface Entertainer ( เสมือนว่า Singer เป็นลูกของ Entertainer) • public class Student implements Entertainer { } หมายถึง คลาส Student ต้องการใช้งาน interface Entertainer (เสมือนว่า Student เป็นลูกของ Entertainer) มองว่ามันคือการสร้างความสัมพันธ์แม่ลูกเหมือน extends ก็ได้

  12. การใช้งาน interface • จากตัวอย่างการเรียกใช้งาน interface ในสไลด์ก่อนหน้า • คลาส Singer กับ คลาส Student เป็นผู้ใช้งาน interface • ทั้งสองคลาสนี้ไม่มีความสัมพันธ์ต่อกันเลย แต่สามารถเรียกใช้งาน interface เดียวกันได้ • และทั้งสองคลาสดังกล่าว ก็ไม่มีความสัมพันธ์ใดเลยกับ interface แต่ก็สามารถเรียกใช้งาน interfaceได้

  13. คลาสหนึ่งๆ สามารถเรียกใช้งานได้หลาย interface • สมมุติมีการสร้าง interface ขึ้นอีกหนึ่งอัน ชื่อว่า Musician ดังนี้ public interface Musician { public abstract void playGuitar() ; public abstract void playPiano() ; } • เราสามารถให้คลาสหนึ่งๆ เรียกใช้งานหลาย interface ได้ เช่น public class Singer implements Entertainer , Musician { } • เป็นการกำหนดให้คลาส Single เรียกใช้งาน interfaceEntertainer และ interface Musician ในคราวเดียวกัน *** นี่คือความแตกต่างอย่างหนึ่งจากการ extends ตามปกติ ****

  14. กฎในการใช้งาน interface • Method ที่อยู่ภายใน interface จะต้องเป็น abstract method เท่านั้น (มีแต่โครงสร้าง ไม่มีรายละเอียดการทำงาน) public interface Musician { public abstract void playGuitar(); public abstract void playPiano(); } public interface Entertrainer { public abstract void sing(){ System.out.prinln(“Sing”) } public abstract void dance(); } โปรแกรม compile ผ่าน Compile error จะแจ้งว่า interface methods cannot have body

  15. กฎในการใช้งาน interface • คลาสใดที่เรียกใช้งาน interface (ระบุ implements หลังชื่อคลาสแล้วตามด้วยชื่อ interface) • ต้อง implement ทุก method ของ interface เหล่านั้นให้ครบถ้วน จึงจะ compile ผ่าน • เนื่องจาก method ของ interface ถูกระบุเป็น public abstract ดังนั้น ตอน implement คลาสที่จะใช้งานก็ต้องระบุ modifier ของ method เหล่านั้นเป็น public ด้วยเช่นกัน

  16. ตัวอย่าง public class SingerClass implements Entertainer , Musician{ public void sing(){ System.out.println("Sing"); } public void dance(){ System.out.println("Dance"); } public void playPiano(){ System.out.println("play piano"); } /*public void playGuitar(){ System.out.println("play guitar"); }*/ } ถ้า implement method ใน interface ไม่ครบ แม้เพียง 1 method โปรแกรมก็จะ compile ไม่ผ่าน ถ้าเราลอง comment method นี้ไว้ โปรแกรมจะ compile error

  17. กฎในการใช้งาน interface • ตัวแปรทุกตัวใน interface ถูกระบุเป็น public static final ดังนั้น คลาสที่มา implements interface จะสามารถเรียกใช้งานตัวแปรได้อย่างเดียว ไม่สามารถเปลี่ยนแปลงค่าได้ รูปแบบการเรียกใช้งานตัวแปรของ interface คือ ชื่อinterface.ชื่อตัวแปร *** ไม่ต้อง get เรียกได้เลย และ ไม่มีสิทธิไป set ค่าของตัวแปรใหม่ เพราะตัวแปรใน interface ทุกตัวเป็น final ***

  18. ตัวอย่าง public class SingerClass implements Entertainer{ public void sing(){ System.out.println("Student can sing"); Entertainer.PRICIPLE = "Some when you are ready"; } public void dance(){ System.out.println("Dance"); } public static void main(String[] args){ System.out.println(Entertainer.PRICIPLE); } } Compile error เพราะพยายามเปลี่ยนแปลงค่าตัวแปร ตัวแปรทุกตัวใน interface กำหนดเป็น final แล้ว ดังนั้น ไม่สามารถเปลี่ยนแปลงค่าได้อีก ตัวอย่างการเรียกใช้ตัวแปรของ interface

  19. กฎในการใช้งาน interface • เราไม่สามารถสร้าง object ของ interface ด้วยคำสั่ง new ได้ public class Test { public static void main(String[] args) { interface e = new Entertainer(); } } Compile error เพราะ intefaceไม่ใช่ class สร้าง object ไม่ได้

  20. กฎในการใช้งาน interface • เราสามารถสร้าง interface ใหม่ที่ inherit จาก interface ที่เคยสร้างไว้แล้วได้ โดยใช้ keyword extends เช่นเดิม • ทำให้เราสามารถใช้งาน abstract method ของ interface แม่ โดยที่ไม่ต้องสร้างขึ้นใหม่เอง • เมื่อคลาสใดมาทำการใช้งาน interface ลูก จะต้อง implements abstract method ทั้งหมด (รวมทั้งที่อยู่ใน interface แม่ด้วย)

  21. ตัวอย่าง สมมุติต้องการสร้าง interface ดังนี้ public interface MobileApplication { public abstract void installMobileBankingApp(); } public interface Iphone3GMpbileApp{ public abstract void installMobileBankingApp(); public abstract void installFacebook(); public abstract void installTwitter(); } Interface Iphone3GMpbileApp มี method ทีทำงานในลักษณะเดียวกับ interface ด้านบน ดังนั้น การออกแบบที่ดีเราจะไม่เขียนไว้ทั้งสองที่

  22. ตัวอย่าง สมมุติต้องการสร้าง interface ดังนี้ public interface MobileApplication { public abstract void installMobileBankingApp(); } public interface Iphone3GMpbileApp extends MobileApplication{ public abstract void installFacebook(); public abstract void installTwitter(); } ทำการ extends จะได้ method ด้านบน โดยไม่ต้องเขียนในคลาสของตนเอง Interface Iphone3GMpbileApp จะมีทั้งหมด 3 method คือ method ที่สืบทอดจาก interface แม่ และ method ของตนเอง

  23. ตัวอย่าง • เมื่อใดที่คลาสใดๆ มีการ implements ไปที่ interface ของคลาสลูก • ต้องกำหนดรายละเอียดการทำงานทุก method รวมทั้งใน interface แม่ด้วย public class Iphone3G implements Iphone3GMobileApp { public void installMobileBankingApp(){ } public void installFacebook(){ } public void installTwitter(){ } } method ที่เขียนไว้ใน interface Iphone3GMobileApp มีเพียง 2 method แต่ต้องเขียนรายละเอียดทั้งหมด 3 method เพราะรวมของ interface แม่ด้วย

  24. abstract class VS. interface • ก่อนหน้านี้เราได้รู้จัก abstract class • เป็นคลาสที่มีบางmethod ที่ประกาศเป็น abstract method • ส่วน interface • ทุกmethod ใน interface ต้องประกาศเป็น abstract method • มันดูคล้ายกันจัง สรุปแล้ว interface แตกต่างจาก abstract class อย่างไร

  25. Interface แตกต่างจาก abstract class อย่างไร

  26. Interface แตกต่างจาก abstract class อย่างไร

  27. ควรใช้ abstract class หรือ interface • หากออกแบบให้ทุก method เป็นแบบ abstract ทั้งหมด กำหนดเป็น interface ดีกว่าที่จะเป็น abstract class • หากคลาสใดมีความสัมพันธ์ในลักษณะที่เป็นคลาสแม่คลาสลูก แบบนี้ก็เหมาะที่จะทำเป็นแบบ abstract class • หากเราต้องการออกแบบในลักษณะที่เป็นลักษณะพื้นฐานทั่วๆไป ไม่ได้ไปสัมพันธ์กับคลาสใดเป็นพิเศษ ก็ออกแบบเป็น interface *** เป็นเพียงข้อแนะนำเบื้องต้น ขึ้นอยู่กับการใช้งานและความเหมาะสม**

More Related