public class pro01 {
public static void main(String[] args) {
Rectangle r1=new Rectangle();
r1.setColor("red");
r1.showAll();
Rectangle r2=new Rectangle(5,2,"blue");
r2.showAll();
Rectangle r3=new Rectangle(12,6,"yellow");
r3.showAll();
}
}
class Rectangle{
private int length;
private int width;
private String color;
//构造方法
Rectangle(){
length=1;
width=1;
color="--";
}
Rectangle(int length,int width,String color){
this.length=length;
this.width=width;
this.color=color;
}
//控制台显示长,宽,面积,周长
public void showAll(){
System.out.println("矩形长为:"+length+"\t"+"矩形的宽为:"+
width+"\t"+"矩形的面积为:"+getArea()+"\t"+"矩形的周长为:"+
getPer()+"\t"+"矩形的颜色为:"+color);
}
//设置颜色
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public double getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public double getHeight() {
return length;
}
public void setHeight(int height) {
this.length = length;
}
//求周长
public int getPer(){
return 2*(length+width);
}
//求面积
public int getArea(){
return length*width;
}
}``
Java输入长、宽、颜色,求矩形面积、周长。
于 2022-11-12 07:33:31 首次发布