应用Rangle1定义子类正方形Square

来源:互联网 发布:淘宝买家退回货物不符 编辑:程序博客网 时间:2024/05/16 00:25

定义矩形接口rect
在接口中有计算面积area()
获取宽度getWidth()
获取高度getHeight()
获取矩形位置getLocation()
继承接口rect实现矩形类Rentangle
编写应用Rangle1定义子类正方形Square

 

//定义矩形接口rect,
interface rect {

    //在接口中有计算面积area()、
    public long area();

    //获取宽度getWidth()、
    public long getWidth();

    //获取高度getHeight()、
    public long getHeight();

    //获取矩形位置getLocation()等的方法。
    public String getLocation();

    public void setHeight(long height);

    public void setWidth(long width);

    public void setX(String x);

    public void setY(String y);
}

//编写继承接口rect实现矩形类Rentangle。
class Rentangle implements rect {

    private long height;
    private long width;

    private String x;
    private String y;

    public long getHeight() {
        return this.height;
    }

    public long getWidth() {
        return this.width;
    }

    public void setHeight(long height) {
        this.height = height;
    }

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

    public void setX(String x) {
        this.x = x;
    }

    public void setY(String y) {
        this.y = y;
    }

    public long area() {
        return this.height*this.width;
    }

    public String getLocation() {
        return "" + this.x + "," + this.y + "";
    }

}

//应用Rangle1类。
public class UseRentangle {
    public static void main(String[] args) {
        rect Rangle1 = new Rentangle();
        Rangle1.setHeight(100);
        Rangle1.setWidth(200);
        Rangle1.setX("10");
        Rangle1.setY("20");
        System.out.println(Rangle1.area());
        System.out.println(Rangle1.getLocation());

        rect square = new Square();
        square.setHeight(100);
        square.setX("30");
        square.setY("50");
        System.out.println(square.area());
        System.out.println(square.getLocation());
    }
}

//编写程序,通过继承Rangle1类定义子类正方形Square。至少编写方法覆盖area(),并实现之。
class Square extends Rentangle {

    private long height;

    private String x;
    private String y;

    public long getHeight() {
        return this.height;
    }

    public long getWidth() {
        return this.height;
    }

    public void setHeight(long height) {
        this.height = height;
    }

    public void setWidth(long height) {
        this.height = height;
    }

    public void setX(String x) {
        this.x = x;
    }

    public void setY(String y) {
        this.y = y;
    }

    public long area() {
        return this.height*this.height;
    }

    public String getLocation() {
        return "" + this.x + "," + this.y + "";
    }

}

原创粉丝点击