java写一个名为Rectangle的类表示矩形。。 其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有: (1) 使用构造函数完成各属性的初始赋值。 (2) 使用get()和set()的形式完成属性的访问及修改。 (3) 提供计算面积的getArea()方法。 (4)创建实例并测试
public class Rectangle {
private double height;
private double width;
private String color;
public Rectangle() {
}
public Rectangle(double height,double width,String color) {
this.color = color;
this.height = height;
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea() {
return width*height;
}
public static void main(String[] args) {
Rectangle rect = new Rectangle(100,100,"red");
System.out.println("area=" + rect.getArea());
}
}
public class Rectangle {
private double width;
private double height;
private String color;
Rectangle(){
this.width=10;
this.height=10;
this.color="red";
}
public void setWidth(double width) {
this.width=width;
}
public void setcolor(String color) {
this.color=color;
}
public void setHeight(double height) {
this.height=height;
}
public String getcolor() {
return color;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() { //计算面积
return width*height;
}
public static void main(String args[]) {
Rectangle r=new Rectangle();
r.setWidth(29);
r.setHeight(30);
r.setcolor("blue");
System.out.println("高 ="+r.getHeight());
System.out.println("宽 ="+r.getWidth());
System.out.println("颜色为:"+r.getcolor());
System.out.println("面积为:"+r.getArea());
}
}