- public class Rectangle
- {
- private String color="红色";
- private double width;
- private double height;
- public void setColor(String color) {
- // 构造法为属性初始化
- this.color=color;
- }
- public void setWidth(double width)
- {
- this.width=width;
- }
- 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 double getLength(){
- // 计算周长
- return 2*(width+height);
- }
- public static void main(String args[])
- {
- Rectangle r=new Rectangle();
- r.setWidth(29);
- r.setHeight(30);
- System.out.println("高 ="+r.getHeight());
- System.out.println("宽 ="+r.getWidth());
- System.out.println("颜色为:"+r.getColor());
- System.out.println("面积为:"+r.getArea());
- System.out.println("周长为:"+r.getLength());
- }
- }
来源: http://www.bubuko.com/infodetail-3206326.html