制作一个矩形类MyRectangle。要求如下:

来源:百度知道 编辑:UC知道 时间:2024/06/30 14:10:35
(1)将MyRectangle类放在一个自己创建的包MyPackage中。
(2)在MyRectangle类中添加width、height属性。
(3)用构造方法对width、height属性进行初始化。
(4)在MyRectangle类中添加成员方法getArea( )计算矩形的面积。
(5)编程利用MyRectangle类的实例输出一个矩形(46,60)的面积。

package MyPackage;
public class MyRectangle {

private float width;
private float height;
public MyRectangle(float width,float heigth){
this.width=width;
this.height=heigth;
}
public float getArea(){
return width*height;
}
public static void main(String[] args) {
MyRectangle m = new MyRectangle(46,60);
System.out.println(m.getArea());
}

}