(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
实验代码:
class test {
private double width;
private double height;
private String color;
public test() {
}
public test(double width, double height, String color) {
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
}
public void setWidth(double w) {
width = w;
}
public void setHeight(double h) {
height = h;
}
public void setColor(String c) {
color = c;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public String getColor() {
return color;
}
public double getArea() {
return width * height;
}
public double getLength() {
return 2 * (width + height);
}
}
public class Rectangle {
public static void main(String[] args) {
test t = null;
t = new test(15, 18, "blue");
System.out.println("周长为"+t.getLength());
System.out.println("面积为"+t.getArea());
}
}
原文:https://www.cnblogs.com/hhl296738954/p/11551470.html