rcp
Administrator
Trong Java một Class bao giờ cũng kế thừa class Object trong package java.lang.
Khi chúng ta muốn kế thừa một class thì dùng từ extends,
thí dụ: public class Circle extends Point
Point.java
public class Point{
Circle.java
public class Circle extends Point { // inherits from Point
Khi chúng ta muốn kế thừa một class thì dùng từ extends,
thí dụ: public class Circle extends Point
Point.java
public class Point{
protected int x, y; // coordinates of the Point
// No-argument constructor
public Point() {
// Constructor with argument
public Point( int a, int b ) {
// Set x and y coordinates of Point
public void setPoint( int a, int b ) {
// get x coordinate
public int getX() { return x; }
// get y coordinate
public int getY() { return y; }
// convert the point into a String representation
public String toString() { return "[" + x + ", " + y + "]"; }
}// No-argument constructor
public Point() {
setPoint( 0, 0 );
}
// Constructor with argument
public Point( int a, int b ) {
setPoint( a, b );
}
// Set x and y coordinates of Point
public void setPoint( int a, int b ) {
x = a;
y = b;
}y = b;
// get x coordinate
public int getX() { return x; }
// get y coordinate
public int getY() { return y; }
// convert the point into a String representation
public String toString() { return "[" + x + ", " + y + "]"; }
Circle.java
public class Circle extends Point { // inherits from Point
protected double radius;
// No-argument constructor
public Circle() { // implicit call to superclass constructor occurs here
// Constructor with argument
public Circle( double r, int a, int b ) {
// Set radius of Circle
public void setRadius( double r ) { radius = ( r >= 0.0 ? r : 0.0 ); }
// Get radius of Circle
public double getRadius() { return radius; }
// Calculate area of Circle
public double area() { return Math.PI * radius * radius; }
// convert the Circle to a String
public String toString() { return "Center = " + "[" + x + ", " + y + "]" +"; Radius = " + radius; }
}// No-argument constructor
public Circle() { // implicit call to superclass constructor occurs here
setRadius( 0 );
}
// Constructor with argument
public Circle( double r, int a, int b ) {
super( a, b ); // call to superclass constructor
setRadius( r );
}setRadius( r );
// Set radius of Circle
public void setRadius( double r ) { radius = ( r >= 0.0 ? r : 0.0 ); }
// Get radius of Circle
public double getRadius() { return radius; }
// Calculate area of Circle
public double area() { return Math.PI * radius * radius; }
// convert the Circle to a String
public String toString() { return "Center = " + "[" + x + ", " + y + "]" +"; Radius = " + radius; }
Sửa lần cuối: