exercise6_1.java

来自「Introduction to java programming 一书中所有编程」· Java 代码 · 共 54 行

JAVA
54
字号
// Exercise6_1.java: Createthe Rectangle class
public class Exercise6_1 {
  public static void main (String[] args) {
    Rectangle myRectangle = new Rectangle(5, 40, "red");
    System.out.println("The area of a rectangle with width " +
      myRectangle.getWidth() + " and height " +
      myRectangle.getHeight() + " is " +
      myRectangle.findArea());
    System.out.println("The color is " + myRectangle.getColor());

    Rectangle yourRectangle = new Rectangle(1.0, 3.5, "yellow");
    System.out.println("The area of a rectangle with width " +
      yourRectangle.getWidth() + " and height " +
      yourRectangle.getHeight() + " is " +
      yourRectangle.findArea());
    System.out.println("The color is " + yourRectangle.getColor());
    System.out.println("The color for myRectangle is " +
      Rectangle.getColor());
  }
}

class Rectangle {
  // Data members
  private double width, height;
  private static String color;

  // Constructor
  public Rectangle(double w, double h, String c) {
    width = w;
    height = h;
    color = c;
  }

  public double getWidth() {
    return width;
  }

  public double getHeight() {
    return height;
  }

  public static String getColor() {
    return color;
  }

  public static void setColor(String color) {
    Rectangle.color = color;
  }

  public double findArea() {
    return width * height;
  }
}

⌨️ 快捷键说明

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