SlideShare une entreprise Scribd logo
1  sur  6
Télécharger pour lire hors ligne
บทที่ 10 การออกแบบ Class แบบ Inheritance และใช้ งาน Garbage Collection

       การออกแบบและสร้าง Class แบบ Inheritance โดยมีการออกแบบ Class ตาม Class Diagram ดงน้ ี
                                                                                       ั
                                       Point
               -   x : int
               -   y : int
               -   static count : int = 0
               +   Point()
               +   Point(int xValue, int yValue)
               +   setX(int xValue) : void
               +   setY(int yValue) : void
               +   getX() : int
               +   getY() : int
               +   static getCount() : int
               +   toString() : String


                                            Rectangle
           -   width : int = 10
           -   height : int = 10
           -   static count : int = 0
           +   Rectangle()
           +   Rectangle(int x, int y, int w, int h)
           +   setWidth(int w) : void
           +   setHeight(int h) : void
           +   getWidth() : int
           +   getHeight() : int
           +   static getCount() : int
           +   toString() : String




การทดลองที่ 10-1
// File Name : Point.java

public class Point {
   private int x = 10; // x part of coordinate pair
   private int y = 10; // y part of coordinate pair
   private static int count = 0;

    // no-argument constructor
    public Point() {
       setX(0);
       setY(0);
       count++;
    }


662305 Information Technology Laboratory II- Chapter 10                         หน ้า 1 จาก 6
// constructor
    public Point( int xValue, int yValue )                {
       setX(xValue);
       setY(yValue);
       count++;
    }

    // finalizer
    protected void finalize() {
       count--;
    }

    // set x in coordinate pair
    public void setX( int xValue ) {
       x = xValue; // no need for validation
    }

    // return x from coordinate pair
    public int getX() {
       return x;
    }

    // set y in coordinate pair
    public void setY( int yValue ) {
       y = yValue; // no need for validation
    }

    // return y from coordinate pair
    public int getY() {
       return y;
    }

    public static int getCount() {
       return count;
    }

    // return String representation of Point object
    public String toString() {
       return "[" + getX() + ", " + getY() + "]";
    }

} // end class Point



// File Name : PointApplet.java

import javax.swing.*;
import java.awt.*;

public class PointApplet extends javax.swing.JApplet {
   int size;
   Point p[] ;


662305 Information Technology Laboratory II- Chapter 10       หน ้า 2 จาก 6
public void init()
    {
       String input; // user's input

          // obtain user's choice
          input = JOptionPane.showInputDialog(
                      "Enter number of point : " );

          size = Integer.parseInt( input );               // convert input to int
          p = new Point[size];

       for(int n = 0 ; n < p.length ; n++) {
          int x = 5 + (int) (Math.random() * 300);
          int y = 5 + (int) (Math.random() * 200);
          p[n] = new Point(x, y);
       }
    } // end method init

    // 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
}


ผลลัพธ์




662305 Information Technology Laboratory II- Chapter 10                   หน ้า 3 จาก 6
การทดลองที่ 10-2
// File Name : Rectangle.java

public class Rectangle extends Point {
   private int width = 10;
   private int height = 10;
   private static int count = 0;

    /** Creates a new instance of Rectangle */
    public Rectangle() {
    }

    public Rectangle(int x, int y, int w, int h ) {
       super(x,y);
       setWidth(w);
       setHeight(h);
    }

    public void setWidth(int w) {
       width = w;
    }

    public void setHeight(int h) {
       height = h;
    }

    public int getWidth() {
       return width;
    }

    public int getHeight() {
       return height;
    }

    public int getArea() {
       return width*height;
    }

662305 Information Technology Laboratory II- Chapter 10   หน ้า 4 จาก 6
public static int getCount() {
       return count;
    }

    protected void finalize() {
       count--;
    }

    public String toString() {
       return "Conner Left = " + super.toString() + "; Width = " +
              getWidth() + "; Height = " + getHeight();
    }
}




// File Name : RectangleApplet.java

import javax.swing.*;
import java.awt.*;

public class RectangleApplet extends javax.swing.JApplet {
   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

    // 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());

662305 Information Technology Laboratory II- Chapter 10           หน ้า 5 จาก 6
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
}

ผลลัพธ์




662305 Information Technology Laboratory II- Chapter 10                หน ้า 6 จาก 6

Contenu connexe

Tendances

Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manualVivek Kumar Sinha
 
Computer graphics
Computer graphics Computer graphics
Computer graphics shafiq sangi
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmKapil Pandit
 
Gauss in java
Gauss in javaGauss in java
Gauss in javabaxter89
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++vidyamittal
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicalsManoj Chauhan
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++Ankit Kumar
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyanAndreeKurtL
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics FunctionsSHAKOOR AB
 
Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ TwitterC4Media
 
Les nouveautés de C# 6
Les nouveautés de C# 6Les nouveautés de C# 6
Les nouveautés de C# 6Microsoft
 
ECMAScript 6 major changes
ECMAScript 6 major changesECMAScript 6 major changes
ECMAScript 6 major changeshayato
 
Class list data structure
Class list data structureClass list data structure
Class list data structureKatang Isip
 

Tendances (20)

Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manual
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
Gauss in java
Gauss in javaGauss in java
Gauss in java
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
662305 11
662305 11662305 11
662305 11
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
My favorite slides
My favorite slidesMy favorite slides
My favorite slides
 
Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ Twitter
 
Les nouveautés de C# 6
Les nouveautés de C# 6Les nouveautés de C# 6
Les nouveautés de C# 6
 
ECMAScript 6 major changes
ECMAScript 6 major changesECMAScript 6 major changes
ECMAScript 6 major changes
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
 

En vedette

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 picoNitigan Nakjuatong
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_dayJanet Hill
 
Chapter 1 PowerPoint
Chapter 1 PowerPointChapter 1 PowerPoint
Chapter 1 PowerPointRadhika Prout
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_dayJanet Hill
 
Presentacion nomina y pss
Presentacion nomina y pssPresentacion nomina y pss
Presentacion nomina y pssluisacomercio2
 
Instructional tech best_practices
Instructional tech best_practicesInstructional tech best_practices
Instructional tech best_practicesRadhika Prout
 
Lowering the drinking age
Lowering the drinking ageLowering the drinking age
Lowering the drinking agemylynn89
 

En vedette (9)

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
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_day
 
Chapter 1 PowerPoint
Chapter 1 PowerPointChapter 1 PowerPoint
Chapter 1 PowerPoint
 
El comite sindical 2001
El comite sindical 2001El comite sindical 2001
El comite sindical 2001
 
Karma 2012
Karma 2012Karma 2012
Karma 2012
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_day
 
Presentacion nomina y pss
Presentacion nomina y pssPresentacion nomina y pss
Presentacion nomina y pss
 
Instructional tech best_practices
Instructional tech best_practicesInstructional tech best_practices
Instructional tech best_practices
 
Lowering the drinking age
Lowering the drinking ageLowering the drinking age
Lowering the drinking age
 

Similaire à 662305 10

namespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docxnamespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docxmaximapikvu8
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfarihantmobileselepun
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdfinfo30292
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Getachew Ganfur
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptSaadAsim11
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbookManusha Dilan
 
oop presentation note
oop presentation note oop presentation note
oop presentation note Atit Patumvan
 

Similaire à 662305 10 (20)

C++ programs
C++ programsC++ programs
C++ programs
 
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docxnamespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
Java practical
Java practicalJava practical
Java practical
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Applications
ApplicationsApplications
Applications
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
Mattbrenner
MattbrennerMattbrenner
Mattbrenner
 
Notes
NotesNotes
Notes
 
Array notes
Array notesArray notes
Array notes
 
oop presentation note
oop presentation note oop presentation note
oop presentation note
 

Plus de Nitigan Nakjuatong (20)

วิธีการกำหนดสิทธิให้กับ 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 5 class_inheritance
Applet 5 class_inheritanceApplet 5 class_inheritance
Applet 5 class_inheritance
 
Applet 7 image_j_panel
Applet 7 image_j_panelApplet 7 image_j_panel
Applet 7 image_j_panel
 
Applet 6 mouse_keyboard
Applet 6 mouse_keyboardApplet 6 mouse_keyboard
Applet 6 mouse_keyboard
 
Applet 5 class_inheritance
Applet 5 class_inheritanceApplet 5 class_inheritance
Applet 5 class_inheritance
 
Applet 4 class_composition
Applet 4 class_compositionApplet 4 class_composition
Applet 4 class_composition
 
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
 
New Assingment3 array2D
New Assingment3 array2DNew Assingment3 array2D
New Assingment3 array2D
 
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
 
Array
ArrayArray
Array
 
Method part2
Method part2Method part2
Method part2
 
Control structure
Control structureControl structure
Control structure
 

662305 10

  • 1. บทที่ 10 การออกแบบ Class แบบ Inheritance และใช้ งาน Garbage Collection การออกแบบและสร้าง Class แบบ Inheritance โดยมีการออกแบบ Class ตาม Class Diagram ดงน้ ี ั Point - x : int - y : int - static count : int = 0 + Point() + Point(int xValue, int yValue) + setX(int xValue) : void + setY(int yValue) : void + getX() : int + getY() : int + static getCount() : int + toString() : String Rectangle - width : int = 10 - height : int = 10 - static count : int = 0 + Rectangle() + Rectangle(int x, int y, int w, int h) + setWidth(int w) : void + setHeight(int h) : void + getWidth() : int + getHeight() : int + static getCount() : int + toString() : String การทดลองที่ 10-1 // File Name : Point.java public class Point { private int x = 10; // x part of coordinate pair private int y = 10; // y part of coordinate pair private static int count = 0; // no-argument constructor public Point() { setX(0); setY(0); count++; } 662305 Information Technology Laboratory II- Chapter 10 หน ้า 1 จาก 6
  • 2. // constructor public Point( int xValue, int yValue ) { setX(xValue); setY(yValue); count++; } // finalizer protected void finalize() { count--; } // set x in coordinate pair public void setX( int xValue ) { x = xValue; // no need for validation } // return x from coordinate pair public int getX() { return x; } // set y in coordinate pair public void setY( int yValue ) { y = yValue; // no need for validation } // return y from coordinate pair public int getY() { return y; } public static int getCount() { return count; } // return String representation of Point object public String toString() { return "[" + getX() + ", " + getY() + "]"; } } // end class Point // File Name : PointApplet.java import javax.swing.*; import java.awt.*; public class PointApplet extends javax.swing.JApplet { int size; Point p[] ; 662305 Information Technology Laboratory II- Chapter 10 หน ้า 2 จาก 6
  • 3. public void init() { String input; // user's input // obtain user's choice input = JOptionPane.showInputDialog( "Enter number of point : " ); size = Integer.parseInt( input ); // convert input to int p = new Point[size]; for(int n = 0 ; n < p.length ; n++) { int x = 5 + (int) (Math.random() * 300); int y = 5 + (int) (Math.random() * 200); p[n] = new Point(x, y); } } // end method init // 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 } ผลลัพธ์ 662305 Information Technology Laboratory II- Chapter 10 หน ้า 3 จาก 6
  • 4. การทดลองที่ 10-2 // File Name : Rectangle.java public class Rectangle extends Point { private int width = 10; private int height = 10; private static int count = 0; /** Creates a new instance of Rectangle */ public Rectangle() { } public Rectangle(int x, int y, int w, int h ) { super(x,y); setWidth(w); setHeight(h); } public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } public int getWidth() { return width; } public int getHeight() { return height; } public int getArea() { return width*height; } 662305 Information Technology Laboratory II- Chapter 10 หน ้า 4 จาก 6
  • 5. public static int getCount() { return count; } protected void finalize() { count--; } public String toString() { return "Conner Left = " + super.toString() + "; Width = " + getWidth() + "; Height = " + getHeight(); } } // File Name : RectangleApplet.java import javax.swing.*; import java.awt.*; public class RectangleApplet extends javax.swing.JApplet { 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 // 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()); 662305 Information Technology Laboratory II- Chapter 10 หน ้า 5 จาก 6
  • 6. 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 } ผลลัพธ์ 662305 Information Technology Laboratory II- Chapter 10 หน ้า 6 จาก 6