// Cylinder.java// 定义Cylinder类public class Cylinder extends Circle {//圆柱继承圆 protected double height; // 圆柱的高度 // 构造函数 public Cylinder() { // 隐含的调用父类的构造函数 setHeight( 0 ); } // 构造函数 public Cylinder( double h, double r, int a, int b ) { super( r, a, b ); // 调用父类的构造函数 setHeight( h ); } // 设置圆柱的高度 public void setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // 获得圆柱的高度 public double getHeight() { return height; } // 计算圆柱的底面积 (i.e., surface area) public double area() { return 2 * super.area() + 2 * Math.PI * radius * height; } // 计算圆柱的体积 public double volume() { return super.area() * height; } // 体积转换成字符串输出 public String toString() { return super.toString() + "; Height = " + height; } // 返回类的名字 public String getName() { return "Cylinder"; }}/************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
java中cylinder_cylinder.java
最新推荐文章于 2024-04-12 15:49:45 发布