cylinder.java

来自「此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码」· Java 代码 · 共 68 行

JAVA
68
字号
// Cylinder.java: The new cylinder class that extends the circle
// class
class Cylinder extends Circle
{
  private double length;

  // Default constructor
  public Cylinder()
  {
    super();
    length = 1.0;
  }

  // Construct a cylinder with specified radius, and length
  public Cylinder(double radius, double length)
  {
    this(radius, "white", 1.0, length);
  }
  
  // Construct a cylinder with specified radius, weight, color, and
  // length
  public Cylinder(double radius,
    String color, double weight, double length)
  {
    super(radius, color, weight);
    this.length = length;
  }

  // Getter method for length
  public double getLength()
  {
    return length;
  }

  // Setter method for length
  public void setLength(double length)
  {
    this.length = length;
  }

  // Find cylinder surface area
  public double findArea()
  {
    return 2*super.findArea()+(2*getRadius()*Math.PI)*length;
  }

  // Find cylinder volume
  public double findVolume()
  {
    return super.findArea()*length;
  }

  // Override the equals() method defined in the Object class
  public boolean equals(Cylinder cylinder)
  {
    return (this.radius == cylinder.getRadius()) &&
      (this.length == cylinder.getLength());
  }

  // Override the toString() method defined in the Object class
  public String toString()
  {
    return "[Cylinder] radius = " + radius + " and length "
      + length;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?