6
āļāļēāļĢāļ­āļ­āļāđāļšāļš Class āđāļšāļš Inheritance āļāļēāļĢāļ­āļ­āļāđāļšāļš Class āđāļšāļš Inheritance āļ„āļ·āļ­ ........................................................................................................................... ............................................................................................................................................................................................ āļ•āļąāļ§āļ­āļĒāļēāļ‡āļāļēāļĢāļ­āļ­āļāđāļšāļš Class āđāļšāļš Inheritance āļˆāļēāļāļĢāļđāļ› āļŠāļēāļĄāļēāļĢāļ–āļ­āļ˜āļīāļšāļēāļĒāđ„āļ”āļ§āļē .................................................................................................................................................. ............................................................................................................................................................................................. Superclass āļ„āļ·āļ­ ................................................................................................................................................................ Subclass āļ„āļ·āļ­ ................................................................................................................................................................ āļ•āļąāļ§āļ­āļĒāļēāļ‡āļāļēāļĢāļŠāļĢāļēāļ‡ 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 :: .....................................................................

Applet 5 class_inheritance

Embed Size (px)

DESCRIPTION

class_inheritance

Citation preview

Page 1: Applet 5 class_inheritance

āļāļēāļĢāļ­āļ­āļāđāļšāļš 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 :: .....................................................................

Page 2: Applet 5 class_inheritance

āļ‚āļąāđ‰āļ™āļ•āļ­āļ™āļ—āđˆāļĩ 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

Page 3: Applet 5 class_inheritance

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

Page 4: Applet 5 class_inheritance

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

Page 5: Applet 5 class_inheritance

āļ‚āļąāđ‰āļ™āļ•āļ­āļ™āļ—āđˆāļĩ 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

Page 6: Applet 5 class_inheritance

āļ‚āļąāđ‰āļ™āļ•āļ­āļ™āļ—āđˆāļĩ 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