【Java】创建一个Rectangle类
添加两个属性width、height,分别表示宽度和高度,添加计算矩形的周长和面积的方法。测试输出一个矩形的周长和面积。
/*作者:龙蝶
*日期:2020年4月1日
*/
public class Rectangle {
private double recWidth;
private double recHeight;
private double recGirth;
private double recArea;
public void getGirth(){
recGirth=2*(recWidth+recHeight);
}
public void getArea(){
recArea=recWidth*recHeight;
}
public void showRec(){
System.out.println("周长:"+recGirth);
System.out.println("面积:"+recArea);
}
public Rectangle(){
Scanner src=new Scanner(System.in);
System.out.print("请输入recWidth和Height:");
int pass=src.nextInt();
int dass=src.nextInt();
this.recWidth=pass;
this.recHeight=dass;
}
public static void main(String[] args){
Rectangle sc=new Rectangle();
sc.getGirth();
sc.getArea();
sc.showRec();
}
}
请输入recWidth和Height:10 20
周长:60.0
面积:200.0