标签:void mamicode main code http 实验代码 out string类 string
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
package xingqisan;
public class Rectangle { //定义Rectangle类
private double width;
private double height;
private String color; //定义属性
public double getHeight() {
return height; //取得height属性
}
public void setHeight(double height) {
this.height = 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 void getArea() {
double area=0;
area=this.height*this.width;
System.out.println("面积为"+area);
}
public void getLength() {
double length=0;
length=(this.height+this.width)*2;
System.out.println("周长为"+length);
}
public static void main(String args[]) {
Rectangle rec=new Rectangle();
rec.setWidth(3);
rec.setHeight(4);
rec.setColor("黄色");
rec.getArea();
rec.getLength();
System.out.println("长:"+rec.getWidth()+",高:"+rec.getHeight()+",颜色:"+rec.getColor());
}
}
标签:void mamicode main code http 实验代码 out string类 string
原文地址:https://www.cnblogs.com/fengmixinluo/p/11541228.html