Upload
nitigan-nakjuatong
View
244
Download
1
Embed Size (px)
DESCRIPTION
class_inheritance
Citation preview
āļāļēāļĢāļāļāļāđāļāļ Class āđāļāļ Inheritance
āļāļēāļĢāļāļāļāđāļāļ 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; ! 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
ssc
Note :: .....................................................................
āļāļąāđāļāļāļāļāļāđāļĩ 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 āđāļāļēāđāļ§āļŠāļģāļŦāļĢāļąāļ Run2) āļŠāļĢāļēāļ 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 Rectangle1) āļŠāļĢāļēāļ testRectangle.html āđāļāļēāđāļ§āļŠāļģāļŦāļĢāļąāļ Run2) āļŠāļĢāļēāļ 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