面向对象的练习题
要求是:定义一个长方形类,定义 求周长和面积的方法,然后定义了一个测试Test,进行测试。
执行代码
//定义一个长方形类,定义 求周长和面积的方法,然后定义了一个测试Test,进行测试。
class Rectangle{
//长
private int length;
//宽
private int width;
1
public Rectangle(int length,int width){
this.length = length;
this.width = width;
}
//求周长
public int getPerimeter(){
return (length + width) * 2;
}
//求面积
public int getArea(){
return length*width;
}
}
class Test{
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 3);
System.out.println("周长: "+ r.getPerimeter()+"面积: "+ r.getArea());
}
}