面向对象入门基础练习
请定义一个描述矩形(MyRectangle)的类,
包含宽和高两个属性,提供获取矩形周长(perimeter)和
面积(area)的方法。并编写测试类进行测试。
`在这里插入package com.itheima;
public class MyRectangle {
private double width;
private double height;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public MyRectangle() {
}
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width+height);
}
}
代码片`
package com.itheima;
public class MyRectangleTest {
public static void main(String[] args) {
MyRectangle mr = new MyRectangle(10,20);
System.out.println("面积:"+mr.getArea());
System.out.println("周长为:"+mr.getPerimeter());
}
}