//Shape.javapublic interface Shape { // calculate area
public abstract double area(); // calculate volume
public abstract double volume(); // return shape name
public abstract String getName();
}//Point.java
public class Point extends Object implements Shape {
protected int x, y; // coordinates of the Point // no-argument constructor
public Point()
{
setPoint( 0, 0 );
} // constructor
public Point( int xCoordinate, int yCoordinate )
{
setPoint( xCoordinate, yCoordinate );
} // Set x and y coordinates of Point
public void setPoint( int xCoordinate, int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
} // get x coordinate
public int getX()
{
return x;
} // get y coordinate
public int getY()
{
return y;
} // convert point into String representation
public String toString()
{
return "[" + x + ", " + y + "]";
} // calculate area
public double area()
{
return 0.0;
} // calculate volume
public double volume()
{
return 0.0;
} // return shape name
public String getName()
{
return "Point";
}} // end class Point//Circle.java
public class Circle extends Point { // inherits from Point
protected double radius; // no-argument constructor
public Circle()
{
// implicit call to superclass constructor here
setRadius( 0 );
} // constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( xCoordinate, yCoordinate ); setRadius( circleRadius );
} // set radius of Circle
public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0 ? circleRadius : 0 );
} // get radius of Circle
public double getRadius()
{
return radius;
} // calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
} // convert Circle to a String represention
public String toString()
{
return "Center = " + super.toString() +
"; Radius = " + radius;
} // return shape name
public String getName()
{
return "Circle";
}} // end class Circle
public abstract double area(); // calculate volume
public abstract double volume(); // return shape name
public abstract String getName();
}//Point.java
public class Point extends Object implements Shape {
protected int x, y; // coordinates of the Point // no-argument constructor
public Point()
{
setPoint( 0, 0 );
} // constructor
public Point( int xCoordinate, int yCoordinate )
{
setPoint( xCoordinate, yCoordinate );
} // Set x and y coordinates of Point
public void setPoint( int xCoordinate, int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
} // get x coordinate
public int getX()
{
return x;
} // get y coordinate
public int getY()
{
return y;
} // convert point into String representation
public String toString()
{
return "[" + x + ", " + y + "]";
} // calculate area
public double area()
{
return 0.0;
} // calculate volume
public double volume()
{
return 0.0;
} // return shape name
public String getName()
{
return "Point";
}} // end class Point//Circle.java
public class Circle extends Point { // inherits from Point
protected double radius; // no-argument constructor
public Circle()
{
// implicit call to superclass constructor here
setRadius( 0 );
} // constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( xCoordinate, yCoordinate ); setRadius( circleRadius );
} // set radius of Circle
public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0 ? circleRadius : 0 );
} // get radius of Circle
public double getRadius()
{
return radius;
} // calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
} // convert Circle to a String represention
public String toString()
{
return "Center = " + super.toString() +
"; Radius = " + radius;
} // return shape name
public String getName()
{
return "Circle";
}} // end class Circle
解决方案 »
- 饿汗式单例是线程安全的吗
- 10进制变成26进制,如何做
- 看coreJava遇到的一个问题?
- JAVA里算法还那么重要么?
- 小虾米诚心求教
- 谁告诉俺个能把classcode转化成机器码的工具,就是把java程序作成*.exe,谢谢
- 判断当前时间是不是在一个时间段里,高手请进
- 大家帮我看看这段程序,为什么运行后,电脑变得很慢。。
- 有没有非教育网下载visualage for java 4.0
- 我从java.sun.com下载了JavaMail,但是为什么没有javax.activation.*;????
- 有没有直接判断输入字符串是否符合IP地址的方法
- Java socket传递对象(object)结果异常,请关注。
public class Cylinder extends Circle {
protected double height; // height of Cylinder
// no-argument constructor
public Cylinder()
{
// implicit call to superclass constructor here
setHeight( 0 );
} // constructor
public Cylinder( double cylinderHeight,
double cylinderRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( cylinderRadius, xCoordinate, yCoordinate ); setHeight( cylinderHeight );
} // set height of Cylinder
public void setHeight( double cylinderHeight )
{
height = ( cylinderHeight >= 0 ? cylinderHeight : 0 );
}
// get height of Cylinder
public double getHeight()
{
return height;
} // calculate area of Cylinder (i.e., surface area)
public double area()
{
return 2 * super.area() + 2 * Math.PI * radius * height;
}
// calculate volume of Cylinder
public double volume()
{
return super.area() * height;
} // convert Cylinder to a String representation
public String toString()
{
return super.toString() + "; Height = " + height;
} // return shape name
public String getName()
{
return "Cylinder";
}} // end class Cylinder
//Test.java
import java.text.DecimalFormat;// Java extension packages
import javax.swing.JOptionPane;public class Test { // test Shape hierarchy
public static void main( String args[] )
{
// create shapes
Point point = new Point( 7, 11 );
Circle circle = new Circle( 3.5, 22, 8 );
Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); // create Shape array
Shape arrayOfShapes[] = new Shape[ 3 ]; // aim arrayOfShapes[ 0 ] at subclass Point object
arrayOfShapes[ 0 ] = point; // aim arrayOfShapes[ 1 ] at subclass Circle object
arrayOfShapes[ 1 ] = circle; // aim arrayOfShapes[ 2 ] at subclass Cylinder object
arrayOfShapes[ 2 ] = cylinder; // get name and String representation of each shape
String output =
point.getName() + ": " + point.toString() + "\n" +
circle.getName() + ": " + circle.toString() + "\n" +
cylinder.getName() + ": " + cylinder.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" ); // loop through arrayOfShapes and get name,
// area and volume of each shape in arrayOfShapes
for ( int i = 0; i < arrayOfShapes.length; i++ ) {
output += "\n\n" + arrayOfShapes[ i ].getName() +
": " + arrayOfShapes[ i ].toString() +
"\nArea = " +
precision2.format( arrayOfShapes[ i ].area() ) +
"\nVolume = " +
precision2.format( arrayOfShapes[ i ].volume() );
} JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 );
}} // end class Test
简化一下:
public interface fruit
{
public abstract colour();
public abstract taste();
pubilc abstract shape();
......................
}public class apple implements fruit
{
public colour()
{}
public taste ()
{}
public shape()
{}
...............
}
用户用起来就会很方便
最具体的例子就是jdbc,java api只不过提供了一套接口,而不用再去管那些数据库驱动怎么实现
而作为我们这些使用者,只要了解了这套api,就可以不管是oracle,sql server还是mysql都可以操作了