不是很会写 可否帮个忙?(初学者)但是这个要交

定义一个类MyRectangle代表矩形,为矩形定义getwidth方法(获得矩形的宽度)。getLength方法(获得矩形的长度)SetWidth(设置宽度)getArea方法(求矩形面积)和toString方法(显示矩形格式)。为矩形派生出一个子类,MySquare代表正方形,并对getArea和toString进行重写。

MyRectangle类会定义吗,把要求中的属性和方法加上就行。


public class MyRectangle {
    private int width;
    private int length;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getArea(){
        return width*length;
    }

    @Override
    public String toString() {
        return "长为:" + length + ", 宽为:" + width;
    }
}
public class MySquare extends MyRectangle{
    @Override
    public int getArea() {
        return super.getArea();
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public static void main(String[] args) {
        MySquare mySquare=new MySquare();
        mySquare.setLength(2);
        mySquare.setWidth(2);
        System.out.println(mySquare);
        System.out.println("面积为:"+mySquare.getArea());
    }
}