งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

การเขียนโปรแกรมภาษาจาวาตอนที่ ๓

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "การเขียนโปรแกรมภาษาจาวาตอนที่ ๓"— ใบสำเนางานนำเสนอ:

1 การเขียนโปรแกรมภาษาจาวาตอนที่ ๓

2 ความรู้เรื่องคลาสและวัตถุ
เมื่อเราได้สร้างคลาสขึ้นมา สิ่งที่เราจะประกาศได้จากคลาสคือ ตัวแปรอ้างอิง (reference) และ วัตถุ (instance) reference instance data data method อ.รัชดาพร คณาวงษ์

3 ตัวอย่างซูเปอร์คลาสและซับคลาส
สมมุติว่าเรามีคลาสเริ่มต้นเป็น คลาสชื่อ Shape บอกลักษณะของรูปเรขาคณิตของวัตถุ 2 มิติ มีคลาส Square บอกลักษณะของสี่เหลี่ยม และคลาส Triangle บอกลักษณะของสามเหลี่ยม ที่สืบทอดลักษณะมาจากคลาส Shape อ.รัชดาพร คณาวงษ์

4 inheritance & overriding
class Figure { double width, height; String name; Figure(double w, double h, String n) { width = w; height = h; name = n; } public String getName() { return name;} public double getArea() { return 0.0;} public void setWidth(double w) { width = w; } public void setHeight(double h) { height = h;} อ.รัชดาพร คณาวงษ์

5 inheritance & overriding
class Rectangle extends Figure { Rectangle(double w, double h) { super(w,h,”rectangle”); } double getArea() { return width*height;} class Triangle extends Figure { Triangle(double w, double h) { super(w,h,”triangle”); double getArea() { return 0.5*widht*height;} อ.รัชดาพร คณาวงษ์

6 Polymorphism class PolyEx { static void compute(Figure x) {
System.out.println(x.getName()+” is”); System.out.println(x.getArea()); } public static void main(String args[]){ compute(new Figure(1, 1, “undefined”); compute(new Triangle(1, 1)); compute(new Rectangle(1, 1)); อ.รัชดาพร คณาวงษ์

7 Polymorphism class A { void print() { System.out.println(“Im A”); } }
class B1 extends A { void print() { System.out.println(“Im B1”);} class B2 extends A { void print() { System.out.println(“Im B2”);} class Other { void call(A a) { a.print(); } public static void main(String args[]){ call(new A()); call(new B1()); call(new B2()); อ.รัชดาพร คณาวงษ์

8 Polymorphism Call(A) print(A) print(B1) print(B2) class A class B1
อ.รัชดาพร คณาวงษ์

9 Astract Classes จะเห็นว่า คลาส Figure เป็นคลาสแม่ ซึ่งไม่ค่อยจะมีรายละเอียดที่แน่นอนเท่าใด อย่างเช่น method getArea ที่คืนค่า 0 เพราะไม่สามารถบอกได้นั่นเอง ดังนั้น เราจึงไม่ควรจะสร้าง instance ใด ๆ จากคลาส Figure Class ที่ไม่เห็นรูปลักษณ์ที่ชัดเจน เช่นนี้ เราสามารถสร้างให้เป็นแม่แบบไว้ก่อนโดยไม่ระบุตัวโปรแกรมได้ ด้วยกลไก Astract Classes อ.รัชดาพร คณาวงษ์

10 Astract Classes ใช้ keyword “abstract” ไว้หน้า class และ method member ของคลาสนั้น Method member ที่มีคำว่า abstract ไม่ต้องมีตัวโปรแกรม Class ใด ๆ ที่มี abstract method อย่างน้อย หนึ่ง ต้องเป็น abstract class เสมอ Abstract Class ไม่สามารถนำไปสร้าง instance ได้ แต่สร้าง reference ได้ อ.รัชดาพร คณาวงษ์

11 Astract Classes ตัวอย่าง abstract class abstract class One {
void printName() { System.out.println(“One”); } abstract void greet(); } abstract class Two { void printName() { System.out.println(“Two”); } อ.รัชดาพร คณาวงษ์

12 Astract Classes ตัวอย่าง การสร้าง class จาก abstract class
class SubOne extends One { void greet() { System.out.println(“Hello”); } } class SubTwo extends Two { } class Abstract2 { static public void main(String args[]) { // new One(); // new Two(); (new SubOne()).greet(); (new SubTwo()).printName(); อ.รัชดาพร คณาวงษ์

13 Astract Classes ตัวอย่างการ implement abstract methods ไม่ครบ
abstract class One { abstract void greet1(); abstract void greet2(); } abstract class Two extends One { void greet1() { System.out.println(“Hello”); } class Three extends Two { void greet2() { System.out.println(“Hi”); } อ.รัชดาพร คณาวงษ์

14 Astract Classes abstract class One { abstract void greet1();
} abstract class Two extends One { void greet1() { System.out.println(“Hello”); } class Three extends Two { void greet2() { System.out.println(“Hi”); } class Abstract3 { static public void main(String args[]) { // new Two(); Three t = new Three(); t.greet1(); t.greet2(); อ.รัชดาพร คณาวงษ์

15 Interfaces คือ abstract classes ที่มีสมาชิก Data member เป็น ค่าคงที่
Method member เป็น abstract อ.รัชดาพร คณาวงษ์

16 Interfaces ใช้คำว่า “interface” แทน “class”
ทุก method ต้องเป็น abstract method และ public ทุก data members ต้องเป็น static และ final interface Greeting { static final public String s = “Hello”; abstract void greet(); } อ.รัชดาพร คณาวงษ์

17 Interfaces เนื่องจาก interface มีได้เพียง abstract method หรือค่าคงที่เท่านั้น ดังนั้นเราจะสร้างคลาสจาก interface ได้โดยใช้คำว่า interface แทนคำว่า extends ส่วนคำว่า extends จะส่งให้ interface เท่านั้น interface extends class extends interface implements class อ.รัชดาพร คณาวงษ์

18 Interface ตัวอย่างการสร้างคลาสจาก interface interface Greeting {
static final String s = “Hello”; abstract void greet(); } class Hello implements Greeting { public void greet() { System.out.println(s); } class Interface1 { public static void main(String args[]) { new Hello().greet(); อ.รัชดาพร คณาวงษ์

19 Interface ตัวอย่างการสร้างคลาสจาก interface
class Hi implements Greeting { public void greet() { System.out.println(s); } void moreGreet() {System.out.println(“How r u?”);} } class Interface2 { static void test(Greeting g) { g.greet();} public static void main(String args[]) { test(new Hello()); test(new Hi()); อ.รัชดาพร คณาวงษ์

20 Note:Interface Note: เราไม่สามารถสร้าง instance ของ interface ดังนั้น data members ต้องมีค่าเริ่มต้น และ ไม่มี constructor อย่างเด็ดขาด อ.รัชดาพร คณาวงษ์

21 Multiple Inheritance ในภาษา C++ ยอมให้มีการกำหนดคลาสที่สืบทอดจากคลาสแม่มากกว่า หนึ่งคลาส ซึ่ง เป็นกลไกที่มีประโยชน์อย่างมาก และสร้างปัญหาอย่างมากเช่นกัน ถ้าไม่ระมัดระวังในการใช้งาน ซึ่งเป็นเหตุให้ Java ไม่ยอมให้มีการทำ multiple inheritance อ.รัชดาพร คณาวงษ์

22 Multiple Inheritance:Problem
คลาส One และ คลาส Two มี int a เป็นสมาชิกทั้งคู่ และ คลาส Three สืบทอดจาก คลาส One และ Two ดังนั้นคำสั่ง cout << a ใน constructor ของ คลาส Three จะเป็น a ที่สืบทอดจากคลาส One หรือคลาส Two ? #include <iostream.h> class One { public: int a = 1; } class Two { public: int a = 2;} class Three : public One, public Two { public: Three() { cout<<a; } } อ.รัชดาพร คณาวงษ์

23 Multiple Inheritance:Problem-Fix
ปัญหาข้างต้นสามารถแก้ไขได้โดยวิธีใส่ Qualified เข้าไป ระบุว่ามาจากคลาส One หรือคลาส Two #include <iostream.h> class One { public: int a = 1; } class Two { public: int a = 2;} class Three : public One, public Two { public: Three() { cout<< One.a; } } อ.รัชดาพร คณาวงษ์

24 Multiple Inheritance:Problem
คลาส Zero มี int a เป็นสมาชิก แล้ว คลาส One และ คลาส Two สืบทอดมาจากคลาส Zero แต่กำหนดค่าให้ a ไม่เท่ากัน ดังนั้น ค่าa ควรจะมีค่าเป็นเท่าใด? #include <iostream.h> Class Zero { public: int a;} class One:public Zero { public: One() { a = 1; } class Two:public Zero { public: Two() { a = 2;} class Three : public One, public Two { public: Three() { cout << a; } } อ.รัชดาพร คณาวงษ์

25 Multiple Inheritance:Problem-Fix
จากตัวอย่างปัญหาข้างต้น Qualified ไม่ช่วยแก้ปัญหาซะแล้วเพราะค่า a จะเป็นเท่าใด ขึ้นกับการสืบทอด ภาษา C++ ก็พยายามแก้ไขปัญหานี้อีกโดยใช้ dynamic inheritance ขึ้น คือเพิ่ม keyword “virtual” เพื่อขยายในตอนรันโปรแกรม และเป็นตามลำดับดังนี้ class Three : public virtual One, public virtual Two { ... } อ.รัชดาพร คณาวงษ์

26 Multiple Inheritance:Problem
การแก้ปัญหาข้างต้น ทำได้แต่เพียงการมองเห็น data member เท่านั้น ซึ่งไม่อาจแก้ปัญหาการ polymorphism method ได้ อ.รัชดาพร คณาวงษ์

27 Multiple Inheritance:Problem
#include <iostream.h> class One { public: virtual void print() { cout << “One”; } }; class Two { public: virtual void print() { cout << “Two”; } class Three : public One, public Two {}; void test (Three x) { x.print(); } void main() { Three t; Test(t); } อ.รัชดาพร คณาวงษ์

28 Multiple Inheritance&Interface
จาวาจึงไม่ยอมให้ทำ multiple inheritance แต่อย่างไรก็ตาม multiple inheritance มีประโยชน์อย่างมาก ดังนั้น จาวาจึงใช้ interface เข้ามาช่วยแก้ปัญหานี้ ภาษาจาวายอมให้ extends ได้จากคลาสเดียว แต่ยอมให้ implements จาก interfaces ได้ไม่จำกัด อ.รัชดาพร คณาวงษ์

29 Multiple Inheritance&Interface
interface Greeting { abstract void greet(); } interface Farewell { abstract void bye(); } class MyClass implements Greeting, Farewell { public void greet() { System.out.println(“Hello”);} public void bye() { System.out.println(“Goodbye”);} } class MulInterface { static void test1(Greeting g) { g.greet();} static void test2(Farewell f) { f.bye();} public static void main(String args[]) { MyClass s = new MyClass(); test1(s); test2(s); อ.รัชดาพร คณาวงษ์

30 Multiple Inheritance&Interface
class Greeting{void greet() {System.out.println(“Hello”);}} interface Farewell { abstract void bye();} class MyClass extends Greeting implements Farewell { public void bye() { System.out.println(“Goodbye”);} } class MulInterface { static void test1(Greeting g) { g.greet();} static void test2(Farewell f) { f.bye();} public static void main(String args[]) { MyClass s = new MyClass(); test1(s); test2(s); อ.รัชดาพร คณาวงษ์

31 Partially implementation
interface Greeting{ abstract void thai(); abstract void english(); } abstract class NotYetGreet implements Greeting { public void thai() { System.out.println(“Ohiyo”);} class Greet extends NoYetGreet { public void english() {System.out.println(“Good morning”);} class PartInterface { public static void main(String args[]) { Greet g = new Greet(); g.thai(); g.english(); อ.รัชดาพร คณาวงษ์

32 Interface extending interface Greeting { abstract void hi();}
interface MoreGreeting extends Greeting { abstract void hello();} class Greet implements MoreGreeting { public void hi() {System.out.print(“Hi”);} public void hello() {System.out.print(“Hello”);} } class extInterface { public static void main(String args[]) { Greet g = new Greet(); g.hi(); g.hello(); อ.รัชดาพร คณาวงษ์

33 Interface without abstract method
interface MyConstants { int YES = 1; double PI = ; String GREET = “Hello”; } class ConsInterface implements MyConstants { public static void main(String args[]){ System.out.println(YES); System.out.println(PI); System.out.println(GREET); อ.รัชดาพร คณาวงษ์

34 ทดสอบ interface A { int x = 1; } interface B { int x = 2; }
class Test1Interface implements A, B{ public static void main(String args[]){ // System.out.println(x); System.out.println(A.x); System.out.println(B.x); } interface A { public void f(); } interface B { public void f(); } class Test2Interface implements A, B{ public void f() { System.out.println(“f”); } public static void main(String args[]){ new Test2().f(); } อ.รัชดาพร คณาวงษ์

35 ทดสอบ interface A { int x = 1; } interface B extends A { int x = 2; }
class Test3Interface implements A, B{ public static void main(String args[]){ System.out.println(x); } interface A { public void f(); } interface B extends A { public void f(); } class Test4Interface implements A, B{ public void f() { System.out.println(“f”); } public static void main(String args[]){ new Test().f(); } อ.รัชดาพร คณาวงษ์

36 ตัวอย่าง สมมุติว่าเราเป็นผู้ผลิตซอฟต์แวร์ตัวหนึ่งชื่อ MyApplication ซึ่งต้องมีการติดตั้งโปรแกรม driver ของเครื่องพิมพ์ได้ด้วย โดยใช้ setPrinter() ดังนั้น เราจึงให้ driver ของเครื่องพิมพ์ ชื่อคลาส printer class MyApplication { Printer p; void setPrinter(Printer p) { this.p = p;} void printOut() { p.print();} } อ.รัชดาพร คณาวงษ์

37 driver printer class เราสร้าง คลาส driver printer แบบ ธรรมดา
class Printer { protected int c = 0; public int getCount() { return c; } public void print() { c++; System.out.println(“Printer”); } อ.รัชดาพร คณาวงษ์

38 Sub classes สมมติมีบริษัท X และ Y ผลิต printer ซึ่งจะนำ class printer driver ของเราไปขยายได้ดังนี้ class Xprinter extends Printer { public void print() { c++; System.out.println(“X Printer”); } class Yprinter extends Printer { public void print() { c++; System.out.println(“Y Printer”); } อ.รัชดาพร คณาวงษ์

39 Polymophism ซึ่งเราก็สามารถเอา driver มาติดตั้งในโปรแกรมของเราได้โดยเขียนดังนี้ class MyApplicationTest { public static void main(String args[]) { MyApplication a = new MyApplication(); a.setPrinter(new Xprinter()); a.printOut(); } อ.รัชดาพร คณาวงษ์

40 driver printer abstract class
หากไม่ใช่ผู้ผลิตเครื่องพิมพ์ การเขียน print() ไม่สามารถทำได้ เพราะไม่ทราบรายละเอียด ดังนั้นการทำเป็น abstract class น่าจะเป็นทางเลือกที่ดีกว่า abstract class Printer { protected int c = 0; public int getCount() { return c; } abstract public void print() } อ.รัชดาพร คณาวงษ์

41 driver printer Interface
การเปิดเผยตัวโปรแกรม อาจจะไม่ใช่สิ่งที่ดีนักในทางธุรกิจ ดังนั้น ถ้าเราจะเพียงแต่ให้ specification design น่าจะดีกว่า interface Printer { int getCount() void print() } อ.รัชดาพร คณาวงษ์

42 driver printer Interface
ทางบริษัทผู้ผลิตเครื่องพิมพ์ ก็สามารถสร้าง คลาสที่ขยายได้ดังนี้ class Xprinter implements Printer { private int c = 0; public int getCount() {return c;} public void print() { c++; System.out.println(“X Printer”); } class Xprinter implements Printer { private int lc = 0; public int getCount() {return (int)lc;} public void print() { c++; System.out.println(“Y Printer”); } อ.รัชดาพร คณาวงษ์


ดาวน์โหลด ppt การเขียนโปรแกรมภาษาจาวาตอนที่ ๓

งานนำเสนอที่คล้ายกัน


Ads by Google