📄 exercise6_1.java
字号:
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -