SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
การออกแบบ Class แบบ Inheritance                                                                                                                                                                 ssc




การออกแบบ Class แบบ Inheritance คือ ...........................................................................................................................
............................................................................................................................................................................................
ตัวอยางการออกแบบ Class แบบ Inheritance




จากรูป สามารถอธิบายไดวา ..................................................................................................................................................
.............................................................................................................................................................................................
Superclass คือ7 ................................................................................................................................................................
Subclass คือ7 ................................................................................................................................................................
ตัวอยางการสราง Class แบบ Inheritance
Class Point
ขั้นตอนที่ 1 สราง Class Point ขึ้นมา
  public class Point {
  }// end class Point

ขั้นตอนที่ 2 สราง Attribute ตางๆ ตามที่ไดออกแบบไว
  !          private int x = 10;                                                          Note :: .....................................................................
  !          private int y = 10;
  !          private static int count = 0;

ขั้นตอนที่ 3 สราง Constructor method ตางๆ ตามที่ไดออกแบบไว
  !          // no-argument constructor
  !          public Point() {
  !          !        setX(0);
  !          !        setY(0);
  !          !        count++;
  !          }
  !          // constructor
  !          public Point( int xValue, int yValue ) {
  !          !        setX(xValue);
  !          !        setY(yValue);
  !          !        count++;
  !          }


Constructor method 2 method นี้มีไวเพื่อ ............................................................................................................................
.............................................................................................................................................................................................
                                                                                                                                                                            Page 1 of 6
ขั้นตอนที่ 4 สราง “set” method ตางๆ ตามที่ไดออกแบบไว
  !          // set       x in coordinate pair
  !          public       void setX( int xValue ) {
  !          !              x = xValue; // no need for validation
  !          }
  !          // set       y in coordinate pair
  !          public       void setY( int yValue ) {
  !          !              y = yValue; // no need for validation
  !          }


“set” method 2 method นี้มีไวเพื่อ ........................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 5 สราง “get” method ตางๆ ตามที่ไดออกแบบไว
  !          // return x from coordinate pair
  !          public int getX() {
  !          !        return x;
  !          }
  !          // return y from coordinate pair
  !          public int getY() {
  !          !        return y;
  !          }


“get” method 2 method นี้มีไวเพื่อ .......................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 6 สราง Facilities method ตางๆ ตามที่ไดออกแบบไว
  !          public static int getCount() {
  !          !        return count;
  !          }
  !          // return String representation of Point object
  !          public String toString() {
  !          !        return "[" + getX() + ", " + getY() + "]";
  !          }!
             // finalizer
  !          protected void finalize() {
  !          !        count--;
  !          }


Facilities method 3 method นี้มีไวเพื่อ .................................................................................................................................
.............................................................................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 7 ทำการ Compile
โดยใชคำสั่ง ..........................................................................................................................................................................
ผลลัพธที่ได ..........................................................................................................................................................................
ขั้นตอนที่ 8 ทดสอบการทำงานของ Class Point เพียง Class เดียวกอน
                      1) สราง testPoint.html เอาไวสำหรับ Run
                      2) สราง testPoint.java เอาไวทดสอบ Class Point
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance/testPoint.html)




                                                                                                                                                                            Page 2 of 6
testPoint.html
  <html>
  !     <body>
          <h1>Test Class Point</h1>
          <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
  !     !     <applet code="testPoint.class" height="200" width="400">
  !     !     </applet>
  !     </body>
  </html>


testPoint.java
ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี
  import java.awt.*;
  import javax.swing.*;

  public class testPoint extends JApplet{
  !     public void init(){
  !     }
  !     public void paint(Graphics g) {
  !     }
  }


method init มีไวเพื่อ .............................................................................................................................................................
method paint มีไวเพื่อ ..........................................................................................................................................................
ขั้นตอนที่ 2 กำหนดคาเริ่มตนที่ method init พรอมกับตัวแปรตางๆ ที่ตองใช
  !          int num;
  !          Point p[] ;


             public void init()
  !          {
  !          !        String input; // user's input
  !          !        // obtain user's choice
  !          !        input = JOptionPane.showInputDialog("Enter number of point : " );
  !          !        num = Integer.parseInt( input ); // convert input to int
  !          !        p = new Point[num];
  !          !        for(int n = 0 ; n < p.length ; n++) {
  !          !        !       int x = 5 + (int) (Math.random() * 400);
  !          !        !       int y = 5 + (int) (Math.random() * 200);
  !          !        !       p[n] = new Point(x, y);
  !          !        }
  !          } // end method init


Note:: ..................................................................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 3 ทดลอง Compile และ Run
วิธีในการ Run คือ .................................................................................................................................................................
ขั้นตอนที่ 4 กำหนดการทำงานใน method paint
  !          // draw shapes on applet's background
  !          public void paint( Graphics g )
  !          {
  !          !        super.paint( g ); //call paint method inherited from JApplet
  !          !        for ( int n = 0; n < p.length; n++ ) {
  !          !        !       // set color
  !          !        !       g.setColor( new Color(255,0,0) );
  !          !        !       // plot point
  !          !        !       g.drawLine( p[n].getX(), p[n].getY(), p[n].getX(), p[n].getY() );
  !          !        } // end for
  !          !        showStatus("จำนวน Object : "+ Point.getCount());
  !          } // end method paint


Note:: ..................................................................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 5 ทดลอง Compile และ Run อีกครั้ง


                                                                                                                                                                            Page 3 of 6
Class Rectangle
ขั้นตอนที่ 1 สราง Class Rectangle ขึ้นมา
  public class Rectangle extends Point {
  }


Note:: ..................................................................................................................................................................................
ขั้นตอนที่ 2 สราง Attribute ตางๆ ตามที่ไดออกแบบไว
  !          private int width = 10;
  !          private int height = 10;
  !          private static int count = 0;

ขั้นตอนที่ 3 สราง Constructor method ตางๆ ตามที่ไดออกแบบไว
  !          public Rectangle() {
  !          }
  !          public Rectangle(int x, int y, int w, int h ) {
  !          !        super(x,y);
  !          !        setWidth(w);
  !          !        setHeight(h);
  !          }


super(x,y); คือ .....................................................................................................................................................................
Constructor method 2 method นี้มีไวเพื่อ ............................................................................................................................
............................................................................................................................................................................................
ขั้นตอนที่ 4 สราง “set” method ตางๆ ตามที่ไดออกแบบไว
  !          public void setWidth(int w) {
  !          !        width = w;
  !          }
  !          public void setHeight(int h) {
  !          !        height = h;
  !          }


“set” method 2 method นี้มีไวเพื่อ ........................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 5 สราง “get” method ตางๆ ตามที่ไดออกแบบไว
  !          public       int getWidth() {
  !          !              return width;
  !          }
  !          public       int getHeight() {
  !          !              return height;
  !          }
  !          public       int getArea() {
  !          !              return width*height;
  !          }
  !          public       static int getCount() {
  !          !              return count;
  !          }


“get” method 4 method นี้มีไวเพื่อ .......................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 6 สราง Facilities method ตางๆ ตามที่ไดออกแบบไว
  !          protected void finalize() {
  !          !        count--;
  !          }
  !          public String toString() {
  !          !        return "Point[x,y]Left = " + super.toString() + "; Width = " +
  !          !        getWidth() + "; Height = " + getHeight();
  !          }


Facilities method 2 method นี้มีไวเพื่อ .................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 7 ทำการ Compile
โดยใชคำสั่ง ..........................................................................................................................................................................
ผลลัพธที่ได ..........................................................................................................................................................................
                                                                                                                                                                            Page 4 of 6
ขั้นตอนที่ 8 ทดสอบการทำงานของ Class Rectangle
              1) สราง testRectangle.html เอาไวสำหรับ Run
              2) สราง testRectangle.java เอาไวทดสอบ Class Rectangle
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance/testRectangle.html)




testRectangle.html
  <html>
  !     <body>
          <h1>Test Class Rectangle</h1>
          <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
  !     !     <applet code="testRectangle.class" height="250" width="400">
  !     !     </applet>
  !     </body>
  </html>


testRectangel.java
ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี
  import java.awt.*;
  import javax.swing.*;

  public class testRectangle extends JApplet{
  !     public void init(){
  !     }
  !     public void paint(Graphics g) {
  !     }
  }


ขั้นตอนที่ 2 กำหนดคาเริ่มตนที่ method init พรอมกับตัวแปรตางๆ ที่ตองใช
  !          int x, y;
  !          Rectangle r ;


  !          public void init()
  !          {
  !          !        String input; // user's input
  !          !        // obtain user's choice
  !          !        input = JOptionPane.showInputDialog("Enter value x                                                       of left point : " );
  !          !        x = Integer.parseInt( input ); // convert input to                                                       int
  !          !        input = JOptionPane.showInputDialog("Enter value y                                                       of left point : " );
  !          !        y = Integer.parseInt( input ); // convert input to                                                       int
  !          !        int w = 10 + (int) (Math.random() * 280);
  !          !        int h = 10 + (int) (Math.random() * 180);
  !          !        r = new Rectangle(x, y, w, h);
  !          } // end method init


Note:: ..................................................................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 3 ทดลอง Compile และ Run

                                                                                                                                                                            Page 5 of 6
ขั้นตอนที่ 4 กำหนดการทำงานใน method paint
  !          // draw shapes on applet's background
  !          public void paint( Graphics g )
  !          {
  !          !        super.paint( g ); //call paint method inherited from JApplet
  !          !        // set color
  !          !        g.setColor( Color.ORANGE );
  !          !        g.drawRect(r.getX(), r.getY(), r.getWidth(),r.getHeight() );
  !          !        g.setColor( Color.BLUE );
  !          !        g.drawString( "Point Left : " + r.getX() + ", " + r.getY(), r.getX(), r.getY());
  !          !        g.drawString( "Width : " + r.getWidth() , r.getX(), r.getY() + 15);
  !          !        g.drawString( "Height : " + r.getHeight() , r.getX(), r.getY() + 30);
  !          !        g.drawString( "Area : " + r.getArea() , r.getX(), r.getY() + 45);
  !          } // end method paint


Note:: ..................................................................................................................................................................................
.............................................................................................................................................................................................
ขั้นตอนที่ 5 ทดลอง Compile และ Run อีกครั้ง


การบาน
แกไขการทำงานของ testRectangle ใหสามารถรับคาจำนวนของ Rectangle ที่จะสรางได
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance-hw/testRectangle.html)




                                                                                                                                                                            Page 6 of 6

Mais conteúdo relacionado

Semelhante a Applet 5 class_inheritance

ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdf
ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdfใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdf
ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdfNattapon
 
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังแบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังKru Wan Mirantee
 
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังแบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังKru Wan Mirantee
 
Manual proteus (thai)
Manual proteus (thai)Manual proteus (thai)
Manual proteus (thai)Apple Nongmui
 
แบบฝึกหัดจำนวนจริงพื้นฐาน
แบบฝึกหัดจำนวนจริงพื้นฐานแบบฝึกหัดจำนวนจริงพื้นฐาน
แบบฝึกหัดจำนวนจริงพื้นฐานNittaya Noinan
 
ไบงานที่ 9
ไบงานที่ 9ไบงานที่ 9
ไบงานที่ 9thatlada5806
 
ไบงานที่ 9
ไบงานที่ 9ไบงานที่ 9
ไบงานที่ 9thatlada5806
 
การเขียนรายงาน
การเขียนรายงานการเขียนรายงาน
การเขียนรายงานDuangsuwun Lasadang
 
Intermediate Java Programming Language (in Thai)
Intermediate Java Programming Language (in Thai)Intermediate Java Programming Language (in Thai)
Intermediate Java Programming Language (in Thai)Thanachart Numnonda
 
Hand-on Exercise Java Web Programming
Hand-on Exercise Java Web Programming Hand-on Exercise Java Web Programming
Hand-on Exercise Java Web Programming IMC Institute
 

Semelhante a Applet 5 class_inheritance (20)

2.circle
2.circle2.circle
2.circle
 
ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdf
ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdfใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdf
ใบงานที่ 2 ประเภทขององค์ประกอบของระบบสารสนเทศ.pdf
 
ใบงาน2
ใบงาน2ใบงาน2
ใบงาน2
 
Computer12
Computer12Computer12
Computer12
 
Sing2
Sing2Sing2
Sing2
 
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังแบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
 
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลังแบบทดสอบบทที่1สมบัติของเลขยกกำลัง
แบบทดสอบบทที่1สมบัติของเลขยกกำลัง
 
Manual proteus (thai)
Manual proteus (thai)Manual proteus (thai)
Manual proteus (thai)
 
รายการสำรวจอุปกรณ์เทคโนโลยีเพื่อการศึกษา
รายการสำรวจอุปกรณ์เทคโนโลยีเพื่อการศึกษารายการสำรวจอุปกรณ์เทคโนโลยีเพื่อการศึกษา
รายการสำรวจอุปกรณ์เทคโนโลยีเพื่อการศึกษา
 
แบบฝึกหัดจำนวนจริงพื้นฐาน
แบบฝึกหัดจำนวนจริงพื้นฐานแบบฝึกหัดจำนวนจริงพื้นฐาน
แบบฝึกหัดจำนวนจริงพื้นฐาน
 
4สารบัญ
4สารบัญ4สารบัญ
4สารบัญ
 
ไบงานที่ 9
ไบงานที่ 9ไบงานที่ 9
ไบงานที่ 9
 
ไบงานที่ 9
ไบงานที่ 9ไบงานที่ 9
ไบงานที่ 9
 
ใบงาน
ใบงานใบงาน
ใบงาน
 
Lesson2
Lesson2Lesson2
Lesson2
 
การเขียนรายงาน
การเขียนรายงานการเขียนรายงาน
การเขียนรายงาน
 
Intermediate Java Programming Language (in Thai)
Intermediate Java Programming Language (in Thai)Intermediate Java Programming Language (in Thai)
Intermediate Java Programming Language (in Thai)
 
Hand-on Exercise Java Web Programming
Hand-on Exercise Java Web Programming Hand-on Exercise Java Web Programming
Hand-on Exercise Java Web Programming
 
Book1-1
Book1-1Book1-1
Book1-1
 
Microsoft Office Word 2007
Microsoft Office Word 2007Microsoft Office Word 2007
Microsoft Office Word 2007
 

Mais de Nitigan Nakjuatong (19)

วิธีการกำหนดสิทธิให้กับ Directory
วิธีการกำหนดสิทธิให้กับ Directoryวิธีการกำหนดสิทธิให้กับ Directory
วิธีการกำหนดสิทธิให้กับ Directory
 
Applet 7 image_j_panel
Applet 7 image_j_panelApplet 7 image_j_panel
Applet 7 image_j_panel
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
Applet 4 class_composition
Applet 4 class_compositionApplet 4 class_composition
Applet 4 class_composition
 
662305 11
662305 11662305 11
662305 11
 
662305 10
662305 10662305 10
662305 10
 
662305 09
662305 09662305 09
662305 09
 
Applet 3 design_class_composition
Applet 3 design_class_compositionApplet 3 design_class_composition
Applet 3 design_class_composition
 
662305 08
662305 08662305 08
662305 08
 
Applet 2 container and action_listener
Applet 2 container and action_listenerApplet 2 container and action_listener
Applet 2 container and action_listener
 
662305 Lab7new
662305 Lab7new662305 Lab7new
662305 Lab7new
 
Assingment3 array2 d
Assingment3 array2 dAssingment3 array2 d
Assingment3 array2 d
 
Lab 6 new
Lab 6 newLab 6 new
Lab 6 new
 
Array2D
Array2DArray2D
Array2D
 
Method part2
Method part2Method part2
Method part2
 
Control structure
Control structureControl structure
Control structure
 
Method JAVA
Method JAVAMethod JAVA
Method JAVA
 
Set putty to use numeric keyboard in pico
Set putty to use numeric keyboard in picoSet putty to use numeric keyboard in pico
Set putty to use numeric keyboard in pico
 
Putty basic setting
Putty basic settingPutty basic setting
Putty basic setting
 

Applet 5 class_inheritance

  • 1. การออกแบบ Class แบบ Inheritance ssc การออกแบบ Class แบบ Inheritance คือ ........................................................................................................................... ............................................................................................................................................................................................ ตัวอยางการออกแบบ Class แบบ Inheritance จากรูป สามารถอธิบายไดวา .................................................................................................................................................. ............................................................................................................................................................................................. Superclass คือ7 ................................................................................................................................................................ Subclass คือ7 ................................................................................................................................................................ ตัวอยางการสราง Class แบบ Inheritance Class Point ขั้นตอนที่ 1 สราง Class Point ขึ้นมา public class Point { }// end class Point ขั้นตอนที่ 2 สราง Attribute ตางๆ ตามที่ไดออกแบบไว ! private int x = 10; Note :: ..................................................................... ! private int y = 10; ! private static int count = 0; ขั้นตอนที่ 3 สราง Constructor method ตางๆ ตามที่ไดออกแบบไว ! // no-argument constructor ! public Point() { ! ! setX(0); ! ! setY(0); ! ! count++; ! } ! // constructor ! public Point( int xValue, int yValue ) { ! ! setX(xValue); ! ! setY(yValue); ! ! count++; ! } Constructor method 2 method นี้มีไวเพื่อ ............................................................................................................................ ............................................................................................................................................................................................. Page 1 of 6
  • 2. ขั้นตอนที่ 4 สราง “set” method ตางๆ ตามที่ไดออกแบบไว ! // set x in coordinate pair ! public void setX( int xValue ) { ! ! x = xValue; // no need for validation ! } ! // set y in coordinate pair ! public void setY( int yValue ) { ! ! y = yValue; // no need for validation ! } “set” method 2 method นี้มีไวเพื่อ ........................................................................................................................................ ............................................................................................................................................................................................. ขั้นตอนที่ 5 สราง “get” method ตางๆ ตามที่ไดออกแบบไว ! // return x from coordinate pair ! public int getX() { ! ! return x; ! } ! // return y from coordinate pair ! public int getY() { ! ! return y; ! } “get” method 2 method นี้มีไวเพื่อ ....................................................................................................................................... ............................................................................................................................................................................................. ขั้นตอนที่ 6 สราง Facilities method ตางๆ ตามที่ไดออกแบบไว ! public static int getCount() { ! ! return count; ! } ! // return String representation of Point object ! public String toString() { ! ! return "[" + getX() + ", " + getY() + "]"; ! }! // finalizer ! protected void finalize() { ! ! count--; ! } Facilities method 3 method นี้มีไวเพื่อ ................................................................................................................................. ............................................................................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 7 ทำการ Compile โดยใชคำสั่ง .......................................................................................................................................................................... ผลลัพธที่ได .......................................................................................................................................................................... ขั้นตอนที่ 8 ทดสอบการทำงานของ Class Point เพียง Class เดียวกอน 1) สราง testPoint.html เอาไวสำหรับ Run 2) สราง testPoint.java เอาไวทดสอบ Class Point ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance/testPoint.html) Page 2 of 6
  • 3. testPoint.html <html> ! <body> <h1>Test Class Point</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testPoint.class" height="200" width="400"> ! ! </applet> ! </body> </html> testPoint.java ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import javax.swing.*; public class testPoint extends JApplet{ ! public void init(){ ! } ! public void paint(Graphics g) { ! } } method init มีไวเพื่อ ............................................................................................................................................................. method paint มีไวเพื่อ .......................................................................................................................................................... ขั้นตอนที่ 2 กำหนดคาเริ่มตนที่ method init พรอมกับตัวแปรตางๆ ที่ตองใช ! int num; ! Point p[] ; public void init() ! { ! ! String input; // user's input ! ! // obtain user's choice ! ! input = JOptionPane.showInputDialog("Enter number of point : " ); ! ! num = Integer.parseInt( input ); // convert input to int ! ! p = new Point[num]; ! ! for(int n = 0 ; n < p.length ; n++) { ! ! ! int x = 5 + (int) (Math.random() * 400); ! ! ! int y = 5 + (int) (Math.random() * 200); ! ! ! p[n] = new Point(x, y); ! ! } ! } // end method init Note:: .................................................................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 3 ทดลอง Compile และ Run วิธีในการ Run คือ ................................................................................................................................................................. ขั้นตอนที่ 4 กำหนดการทำงานใน method paint ! // draw shapes on applet's background ! public void paint( Graphics g ) ! { ! ! super.paint( g ); //call paint method inherited from JApplet ! ! for ( int n = 0; n < p.length; n++ ) { ! ! ! // set color ! ! ! g.setColor( new Color(255,0,0) ); ! ! ! // plot point ! ! ! g.drawLine( p[n].getX(), p[n].getY(), p[n].getX(), p[n].getY() ); ! ! } // end for ! ! showStatus("จำนวน Object : "+ Point.getCount()); ! } // end method paint Note:: .................................................................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 5 ทดลอง Compile และ Run อีกครั้ง Page 3 of 6
  • 4. Class Rectangle ขั้นตอนที่ 1 สราง Class Rectangle ขึ้นมา public class Rectangle extends Point { } Note:: .................................................................................................................................................................................. ขั้นตอนที่ 2 สราง Attribute ตางๆ ตามที่ไดออกแบบไว ! private int width = 10; ! private int height = 10; ! private static int count = 0; ขั้นตอนที่ 3 สราง Constructor method ตางๆ ตามที่ไดออกแบบไว ! public Rectangle() { ! } ! public Rectangle(int x, int y, int w, int h ) { ! ! super(x,y); ! ! setWidth(w); ! ! setHeight(h); ! } super(x,y); คือ ..................................................................................................................................................................... Constructor method 2 method นี้มีไวเพื่อ ............................................................................................................................ ............................................................................................................................................................................................ ขั้นตอนที่ 4 สราง “set” method ตางๆ ตามที่ไดออกแบบไว ! public void setWidth(int w) { ! ! width = w; ! } ! public void setHeight(int h) { ! ! height = h; ! } “set” method 2 method นี้มีไวเพื่อ ........................................................................................................................................ ............................................................................................................................................................................................. ขั้นตอนที่ 5 สราง “get” method ตางๆ ตามที่ไดออกแบบไว ! public int getWidth() { ! ! return width; ! } ! public int getHeight() { ! ! return height; ! } ! public int getArea() { ! ! return width*height; ! } ! public static int getCount() { ! ! return count; ! } “get” method 4 method นี้มีไวเพื่อ ....................................................................................................................................... ............................................................................................................................................................................................. ขั้นตอนที่ 6 สราง Facilities method ตางๆ ตามที่ไดออกแบบไว ! protected void finalize() { ! ! count--; ! } ! public String toString() { ! ! return "Point[x,y]Left = " + super.toString() + "; Width = " + ! ! getWidth() + "; Height = " + getHeight(); ! } Facilities method 2 method นี้มีไวเพื่อ ................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 7 ทำการ Compile โดยใชคำสั่ง .......................................................................................................................................................................... ผลลัพธที่ได .......................................................................................................................................................................... Page 4 of 6
  • 5. ขั้นตอนที่ 8 ทดสอบการทำงานของ Class Rectangle 1) สราง testRectangle.html เอาไวสำหรับ Run 2) สราง testRectangle.java เอาไวทดสอบ Class Rectangle ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance/testRectangle.html) testRectangle.html <html> ! <body> <h1>Test Class Rectangle</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testRectangle.class" height="250" width="400"> ! ! </applet> ! </body> </html> testRectangel.java ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import javax.swing.*; public class testRectangle extends JApplet{ ! public void init(){ ! } ! public void paint(Graphics g) { ! } } ขั้นตอนที่ 2 กำหนดคาเริ่มตนที่ method init พรอมกับตัวแปรตางๆ ที่ตองใช ! int x, y; ! Rectangle r ; ! public void init() ! { ! ! String input; // user's input ! ! // obtain user's choice ! ! input = JOptionPane.showInputDialog("Enter value x of left point : " ); ! ! x = Integer.parseInt( input ); // convert input to int ! ! input = JOptionPane.showInputDialog("Enter value y of left point : " ); ! ! y = Integer.parseInt( input ); // convert input to int ! ! int w = 10 + (int) (Math.random() * 280); ! ! int h = 10 + (int) (Math.random() * 180); ! ! r = new Rectangle(x, y, w, h); ! } // end method init Note:: .................................................................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 3 ทดลอง Compile และ Run Page 5 of 6
  • 6. ขั้นตอนที่ 4 กำหนดการทำงานใน method paint ! // draw shapes on applet's background ! public void paint( Graphics g ) ! { ! ! super.paint( g ); //call paint method inherited from JApplet ! ! // set color ! ! g.setColor( Color.ORANGE ); ! ! g.drawRect(r.getX(), r.getY(), r.getWidth(),r.getHeight() ); ! ! g.setColor( Color.BLUE ); ! ! g.drawString( "Point Left : " + r.getX() + ", " + r.getY(), r.getX(), r.getY()); ! ! g.drawString( "Width : " + r.getWidth() , r.getX(), r.getY() + 15); ! ! g.drawString( "Height : " + r.getHeight() , r.getX(), r.getY() + 30); ! ! g.drawString( "Area : " + r.getArea() , r.getX(), r.getY() + 45); ! } // end method paint Note:: .................................................................................................................................................................................. ............................................................................................................................................................................................. ขั้นตอนที่ 5 ทดลอง Compile และ Run อีกครั้ง การบาน แกไขการทำงานของ testRectangle ใหสามารถรับคาจำนวนของ Rectangle ที่จะสรางได ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/inheritance-hw/testRectangle.html) Page 6 of 6