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

解决方案 »

  1.   

    //Cylinder.java
    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
      

  2.   

    这么长的例子怎么看?
    简化一下:
    public interface fruit 
    {
        public abstract colour();
        public abstract taste();
        pubilc abstract shape();
        ...................... 
    }public class apple implements fruit
    {
        public colour()
        {}
        public taste ()
        {}
        public shape()
        {}
        ...............
    }
      

  3.   

    接口可以提供一套标准,而不必在乎开发这怎么实现
    用户用起来就会很方便
    最具体的例子就是jdbc,java api只不过提供了一套接口,而不用再去管那些数据库驱动怎么实现
    而作为我们这些使用者,只要了解了这套api,就可以不管是oracle,sql server还是mysql都可以操作了
      

  4.   

    哈哈,最好看看java的Collection架构,一个List接口有许多种具体实现,而对List的操作却非常统一。也就是说你可以随意变更自己的实现类,而只需保证接口不便就可以了。