JAVA學習筆記(十三)-模板方法模式Template,學習筆記template

來源:互聯網
上載者:User

JAVA學習筆記(十三)-模板方法模式Template,學習筆記template
模板方法模式Template

/* * 功能:實現數組的排序和列印 *  * 定義:在抽象類別中定義一個演算法(功能)的架構,將具體的步驟延遲到子類中實現。 * 組成:一個抽象類別、一個或多個實作類別 * 抽象類別中包含: * 1.抽象方法,只定義規範,由子類來實現 * 2.模板方法,由父類實現,調用抽象方法來完成邏輯功能,一般定義為final方法,不允許子類重寫 *  * 優點: * 1.可擴充性,添加不同的實作類別實現擴充 * 2.便於維護 *  * 應用場合:多個子類擁有相同的方法,並且這些方法的邏輯功能相同時,建議使用模板方法模式 */public abstract class Test03{    /*     * 抽象方法,將數組按從大到小排序     */    public abstract void sort(int[] nums);    /*     * 模板方法:列印排序後的數組     */    public final void print(int[] nums)    {        this.sort(nums);// 對數組進行排序        System.out.println("排序後的數組:");        for (int i = 0; i < nums.length; i++)        {            System.out.print(nums[i] + " ");        }    }}

測試類別

/* * 測試類別 */public class Test{    public static void main(String[] args)    {        int[] nums = { 12, 45, 2, 130, 43, 76, 3, 56 };        Test03 test = new SubTest03();        test.print(nums);    }}

繼承自Test03

/* * 繼承自Test03 */public class SubTest03 extends Test03{    // 重寫了父類的sort方法    public void sort(int[] nums)    {        for (int i = 0; i < nums.length - 1; i++)        {            for (int j = 0; j < nums.length - 1 - i; j++)            {                if (nums[j] < nums[j + 1])                {                    int temp = nums[j];                    nums[j] = nums[j + 1];                    nums[j + 1] = temp;                }            }        }    }}
模板方法執行個體
/* * 使用模式方法模式,實現計算體積 */public class Test04{    public static void main(String[] args)    {        Volume v = new Cylinder(5.3, 4);        System.out.println("圓柱體的體積:" + v.getVolume());        v = new Cuboid(5, 3.2, 6.4);        System.out.println("長方體的體積:" + v.getVolume());    }}/* * 體積抽象類別 */abstract class Volume{    private double height;// 高    public Volume(double height)    {        this.height = height;    }    public double getHeight()    {        return height;    }    // 抽象方法:計算面積    public abstract double getArea();    // 模板方法:計算體積    public double getVolume()    {        return getArea() * height;    }}/* * 圓柱體類Cylinder */class Cylinder extends Volume{    private double r;// 半徑    public Cylinder(double height, double r)    {        super(height);        this.r = r;    }    // 重寫父類的方法    @Override    public double getArea()    {        return Math.PI * r * r;// 計算圓的面積    }}/* * 長方體類Cuboid */class Cuboid extends Volume{    private double length;// 長    private double width;// 寬    public Cuboid(double height, double length, double width)    {        super(height);        this.length = length;        this.width = width;    }    // 重寫父類的方法    @Override    public double getArea()    {        return length * width;// 計算長方形的面積    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.