天天看點

Java知識(010)--深入面向對象

深入面向對象

  1. 繼承:從已有的類建立新類的過程,提供繼承資訊的類稱為父類(超類、基類),得到繼承資訊的類稱為子類(派生類、衍生類)。繼承使用extends關鍵字,Java中的繼承是單繼承(一個類之隻能有一個父類)。
  2. 多态:子類在繼承父類的過程中可以對父類已有的方法進行重寫,不同的子類給出不同的實作版本,那麼同樣類型的引用調用同樣的方法将發生不同的行為,這就是多态。
  3. 相關概念
    • 抽象類:被abstract關鍵字修飾的類,抽象類不能執行個體化(不能建立對象),專門為其他類提供繼承資訊。
    • 抽象方法:如果一個方法沒有方法體,就可以定義為抽象方法,也是用abstract關鍵字修飾,如果一個類有抽象方法,這個類必須被聲明為抽象類。子類繼承該抽象類時必須重寫抽象方法。
    • 終結類:被final關鍵字修飾的類,終結類不能被繼承,工具類通常聲明為終結類。
    • 終結方法:被final關鍵字修飾的方法,子類中不能重寫終結方法。
    • 靜态方法:被static修飾的方法,靜态的方法和屬性屬于類,不屬于對象,在記憶體中隻有唯一的拷貝,靜态方法和屬性調用的方式是用類名調用,而不是通過對象的引用來調用。
  4. 接口:在Java中,接口是實作可插入特性的保證。定義接口的關鍵字是interface,實作接口的關鍵字是implements,一個類可以實作多個接口,接口之間的繼承支援多重繼承。
  5. 接口和抽象類的異同:
    • 相同:抽象類和接口都不可以執行個體化;抽象類和接口中的方法都可以沒有方法體;抽象類和接口的子類都可以強轉成父類的類型;抽象類和接口的子類都必須實作父類中的抽象方法,除非子類也聲明成抽象類或接口。
    • 不同點:抽象類隻能被單繼承,接口可以多實作;接口中的變量必須是final的(如果沒寫,預設也是final的),抽象類則沒有;抽象類中的方法可以有有方法體的,接口則不可以;接口在繼承的時候使用implements關鍵字,表示實作,抽象類則用extends關鍵字,表示繼承。
  6. 類/類和類/接口之間的關系
    • IS-A關系:繼承/實作
    • HAS-A關系:關聯/聚合(聚集)/合成
    • USE-A關系:依賴
  7. UML:統一模組化語言(标準的圖形化符号)
    • 類圖:描述類以及類和類之間關系的圖形符号。
    • 用例圖:捕獲需求。
    • 時序圖:描述對象互動關系。
  8. 面向對象的設計原則
    • 單一職責原則(SRP):類的設計要做到高内聚,一個類隻承擔單一的職責(不做不該它做的事情)。
    • 開閉原則(OCP):軟體系統應該接受擴充(對擴充開放),不接受修改(對修改關閉)。要符合開閉原則:抽象是關鍵,封裝可變性。
    • 依賴倒轉原則(DIP):面向接口程式設計。聲明變量的引用類型,聲明方法的參數類型,聲明方法的傳回類型時,盡可能使用抽象類型而不是具體類型。
    • 裡氏替換原則(LSP):用子類型替換父類型沒有任何問題,用父類型替換子類型通常都是不行的。
    • 接口隔離原則(ISP):接口要小而專,不能大而全。
    • 合成聚合複用原則(CARP):優先考慮用強關聯關系複用代碼,而不是用繼承關系複用代碼。
    • 迪米特法則(LoD):對象之間盡可能少的發生聯系。
package jerry;

import java.awt.Graphics;

/**
 * 能否在視窗上繪圖的接口
 * @author HK
 *
 */
public interface Drawable {

    /**
     * 繪圖接口
     * @param g 畫筆對象
     */
    public abstract void draw(Graphics g);
}
           
package jerry;

import java.awt.Color;

/**
 * 幾何圖形(抽象類)
 * @author HK
 *
 */
public abstract class Shape implements Drawable {
    protected int x;            // 中心的橫坐标
    protected int y;            // 中心的縱坐标
    protected Color color;      // 顔色

    /**
     * 計算面積
     * @return 圖形的面積
     */
    public abstract double area();

    /**
     * 計算周長
     * @return 圖形的周長
     */
    public abstract double perimeter();

    public void setColor(Color color) {
        this.color = color;
    }

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

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

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
           
package jerry;

import java.awt.Graphics;

/**
 * 圓
 * @author HK
 *
 */
public class Circle extends Shape {
    private int radius;     // 半徑

    /**
     * 構造器
     * @param radius 半徑
     */
    public Circle(int radius) {
        this.radius = radius;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawOval(x - radius, y - radius,  * radius,  * radius);
    }
   /**
    *圓的面積
    */
    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
    /**
    *
    *圓的周長
    */
    @Override
    public double perimeter() {
        return  * Math.PI * radius;
    }

}
           
package jerry;

import java.awt.Graphics;
/**
*
*四邊形
*@author HK
*/
public class Rect extends Shape {
    private int width;      // 寬
    private int height;     // 高

    /**
     * 構造器
     * @param width 寬
     * @param height 高
     */
    public Rect(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawRect(x - width / , y - height / , width, height);
    }
    /**
    *
    *四邊形面積
    */
    @Override
    public double area() {
        return width * height;
    }
    /**
    *
    *四邊形周長
    */
    @Override
    public double perimeter() {
        return (width + height) << ;
    }
}
           
package jerry;

import java.awt.Graphics;

/**
 * 等邊三角形
 * @author HK
 *
 */
public class Triangle extends Shape {
    private int edge;//邊長

    /**
     * 構造器
     * @param edge 邊長
     */
    public Triangle(int edge) {
        this.edge = edge;
    }

    @Override
    public void draw(Graphics g) {
        int ax = x;
        int ay = (int) (y - edge * Math.cos(Math.PI / ) 
                + edge /  * Math.tan(Math.PI / ));
        int bx = x - edge / ;
        int by = (int) (y + edge /  * Math.tan(Math.PI / ));
        int cx = x + edge / ;
        int cy = (int) (y + edge /  * Math.tan(Math.PI / ));

        g.setColor(color);
        g.drawLine(ax, ay, bx, by);
        g.drawLine(bx, by, cx, cy);
        g.drawLine(cx, cy, ax, ay);
    }
    /**
    *
    *三角形的面積
    */
    @Override
    public double area() {
        double s = perimeter() / ;
        return Math.sqrt(s * Math.pow(s - edge, ));
    }
    /**
    *
    *三角形周長
    */
    @Override
    public double perimeter() {
        return  * edge;
    }

}
           
package jerry;

import java.awt.Color;

/**
 * 自定義工具類
 * @author HK
 *
 */
public final class MyUtil {

    private MyUtil() {
    }

    /**
     * 産生指定範圍的随機整數
     * @param min 最小值(閉區間)
     * @param max 最大值(閉區間)
     * @return 指定範圍的随機整數
     */
    public static int random(int min, int max) {
        return (int) (Math.random() * (max - min + ) + min);
    }

    /**
     * 生成随機顔色
     * @return Color對象
     */
    public static Color randomColor() {
        int r = random(, );
        int g = random(, );
        int b = random(, );
        return new Color(r, g, b);
    }
}
           
package jerry;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class DrawingFrame extends JFrame implements ActionListener {
    private List<Shape> shapeList = new ArrayList<Shape>();//容器
    private List<Shape> redoList = new ArrayList<Shape>();

    private JButton circleButton, rectButton, triButton;// 按鈕
    private JButton undoButton, redoButton, clearButton;

    private Shape shape = null;
    private String shapeType = "Circle";

    public DrawingFrame() {
        this.setSize(, );//視窗大小
        this.setResizable(false);//大小不可改
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        circleButton = new JButton("Circle");
        circleButton.addActionListener(this);
        rectButton = new JButton("Rectangle");
        rectButton.addActionListener(this);
        triButton = new JButton("Triangle");
        triButton.addActionListener(this);

        undoButton = new JButton("撤銷");
        undoButton.addActionListener(this);
        redoButton = new JButton("恢複");
        redoButton.addActionListener(this);
        clearButton = new JButton("清空");
        clearButton.addActionListener(this);

        this.setLayout(new FlowLayout());
        this.add(circleButton);
        this.add(rectButton);
        this.add(triButton);
        this.add(undoButton);
        this.add(redoButton);
        this.add(clearButton);

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if(shapeType.equals("Circle")) {
                    int radius = MyUtil.random(, );
                    shape = new Circle(radius);
                }
                else if(shapeType.equals("Rectangle")) {
                    int width = MyUtil.random(, );
                    int height = MyUtil.random(, );
                    shape = new Rect(width, height);
                }
                else if(shapeType.equals("Triangle")) {
                    int edge = MyUtil.random(, );
                    shape = new Triangle(edge);
                }

                if(shape != null) {
                    shape.setX(e.getX());
                    shape.setY(e.getY());
                    Color color = MyUtil.randomColor();
                    shape.setColor(color);
                    shapeList.add(shape);

                    repaint();
                }
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for(Shape tempShape : shapeList) {
            tempShape.draw(g);
            g.drawString(
                String.format("周長: %.2f", tempShape.perimeter()), 
                tempShape.getX(), tempShape.getY());
            g.drawString(
                String.format("面積: %.2f", tempShape.area()), 
                tempShape.getX(), tempShape.getY() + );
        }
    }

    public static void main(String[] args) {
        new DrawingFrame().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == circleButton) {
            shapeType = "Circle";
        }
        else if(e.getSource() == rectButton) {
            shapeType = "Rectangle";
        }
        else if(e.getSource() == triButton) {
            shapeType = "Triangle";
        }
        else if(e.getSource() == undoButton) {
            if(shapeList.size() > ) {
                Shape temp = shapeList.remove(shapeList.size() - );
                redoList.add(temp);
                repaint();
            }
        }
        else if(e.getSource() == redoButton) {
            if(redoList.size() > ) {
                Shape temp = redoList.remove(redoList.size() - );
                shapeList.add(temp);
                repaint();
            }
        }
        else if(e.getSource() == clearButton) {
            shapeList.clear();
            repaint();
        }
    }
}
           

繼續閱讀