《Oracle Java SE编程指南》13-01:为什么要使用

2020-04-27  本文已影响0人  AT阿宝哥
课程封面-JavaSE-AT阿宝哥

内容导航


1、问题

假定我们要使用Java编程语言在计算机上开发绘图相关的功能,比如绘制圆形(Circle)、矩形(Rectangle)和三角形(Triangle)等等。但不管是何种图形,在绘制之前,都需要确定被绘制图形的起始坐标,即xy的值。其次,计算机肯定不会自动绘图,故每种图形都需要绘制方法来描述绘图过程。尽管不同的图形,绘制的过程可能不一样。


2、实现方案

2.1、绘制圆形

除了起始坐标xy,以及绘制方法draw()外,圆形有特殊的属性半径r,还有获取直径的方法getDiameter()

示例代码:


//类:圆形
public class Circle {

    // 属性:x坐标
    private int x;

    // 属性:y坐标
    private int y;

    // 属性:半径
    private int r;

    // 构造器:无参的
    public Circle() {

    }

    // 方法:绘图
    public void draw() {

        // 绘制圆形的算法

    }

    // 方法:获取直径
    public int getDiameter() {

        return 2 * r;
    }

    public int getX() {
        return x;
    }

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

    public int getY() {
        return y;
    }

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

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

}

2.2、绘制矩形

除了起始坐标xy,以及绘制方法draw()外,矩形有特殊的属性宽度w和高度h

示例代码:


//类:矩形
public class Rectangle {

    // 属性:x坐标
    private int x;

    // 属性:y坐标
    private int y;

    // 属性:w宽度
    private int w;

    // 属性:h高度
    private int h;

    // 构造器:无参的
    public Rectangle() {

    }

    // 方法:绘图
    public void draw() {

        // 绘制矩形的算法

    }

    public int getX() {
        return x;
    }

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

    public int getY() {
        return y;
    }

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

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

}


3、方案分析

通过对比上述代码,我们发现属性xyGettersSetters、方法draw() 存在冗余,并且随着待定义图形类型数量的增加呈线性增长趋势,这会给代码维护造成很大的麻烦,您期望这种情况延续吗?

示例代码:


    // 属性:x坐标
    private int x;

    // 属性:y坐标
    private int y;

    // 方法:绘图
    public void draw() {

        // 绘制的算法

    }

    //Getters与Setters

    public int getX() {
        return x;
    }

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

    public int getY() {
        return y;
    }

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


4、改进方案

圆形、矩形和三角形都被称为图形,那不妨定义一个独立的图形类Shape,且拥有和上述三种图形相同的属性xyGettersSetters、方法draw() ,并设法实现Shape类的复用从而消除冗余。

示例代码—图形类:


//类:图形
public class Shape {

    // 属性:x坐标
    private int x;

    // 属性:y坐标
    private int y;

    // 构造器:无参的
    public Shape() {

    }

    // 方法:绘图
    public void draw() {
        // 绘制图形的算法
    }

    public int getX() {
        return x;
    }

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

    public int getY() {
        return y;
    }

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

}

示例代码—圆形类:


//类:圆形
public class Circle {

    // 属性:半径
    private int r;

    // 构造器:无参的
    public Circle() {

    }

    // 方法:绘图
    public void draw() {

        // 绘制圆形的算法

    }

    // 方法:获取直径
    public int getDiameter() {

        return 2 * r;
    }

}

示例代码—图形类:


//类:矩形
public class Rectangle {

    // 属性:w宽度
    private int w;

    // 属性:h高度
    private int h;

    // 构造器:无参的
    public Rectangle() {

    }

    // 方法:绘图
    public void draw() {

        // 绘制矩形的算法

    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

}

这种神奇的方法便是继承,我们在下一章节详述。


持续更新,欢迎留言提议!
码字很累,多点赞多赞赏!


扫描二维码,关注AT阿宝哥
上一篇下一篇

猜你喜欢

热点阅读