标题: 定义一个长方形类,定义求周长和面积的方法,然后定义一个测试类进行测试。 [打印本页] 作者: lt3886930 时间: 2016-6-9 10:29 标题: 定义一个长方形类,定义求周长和面积的方法,然后定义一个测试类进行测试。 import java.util.Scanner;
class Demo_Rectangle {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.println("请输入长方形的高: ");
int high = sr.nextInt();
System.out.println("请输入长方形的宽:");
int width = sr.nextInt();
Rectangle r = new Rectangle(high,width);
r.show();
}
}
class Rectangle {
private int high;
private int width;
public Rectangle(){}
public Rectangle(int high, int width){
this.high = high;
this.width = width;
}
public void setHigh(int high){
this.high = high;
}
public int getHigh(){
return high;
}
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public int getLength(){
return (width+high)*2;
}
public int getArea(){
return width*high;
}