模板模式和Comparable類

來源:互聯網
上載者:User

標籤:style   blog   color   os   ar   java   for   sp   div   

模板模式中,父類規定好了一些演算法的流程,並且空出一些步驟(方法)留給子類填充

Java的數組類中靜態方法sort()就是一個模板,它空出了一個compareTo的方法,留給子類填充,用來規定什麼是大於、等於和小於

 

package com.example.template;public class Duck implements Comparable<Duck>{    private String name;    private int weight;        public Duck(String name, int weight){        this.name = name;        this.weight = weight;    }        public String toString(){        return "name" + name + "weight" + weight;    }        @Override    public int compareTo(Duck other) {        if(this.weight < other.weight){            return -1;        }else if(this.weight > other.weight){            return 1;        }                return 0;    }}
package com.example.template;import java.util.Random;public class Employee implements Comparable<Employee> {    private String name;    private int id;    private double salary;        public Employee(String name, double salary){        this.name = name;        this.salary = salary;        Random ra = new Random();        this.id = ra.nextInt(1000000);    }        public String toString(){        return "name:" + name + " salary:" + salary + " ElyId:" + id;    }        @Override    public int compareTo(Employee other) {        if(this.id < other.id){            return -1;        }else if(this.id > other.id){            return 1;        }        return 0;    }}
package com.example.template;import java.util.Arrays;public class Test {    public static void main(String[] args)    {        Duck[] ducks = new Duck[4];        ducks[0] = new Duck("Jason", 89);        ducks[1] = new Duck("Amemle", 100);        ducks[2] = new Duck("Mike", 72);        ducks[3] = new Duck("Ham", 102);                System.out.println("before sort:");        for(int i = 0 ; i < ducks.length ; i++)            System.out.println(ducks[i].toString());                Arrays.sort(ducks);                System.out.println("after sort:");        for(int i = 0 ; i < ducks.length ; i++)            System.out.println(ducks[i].toString());                Employee[] emps = {            new Employee("Jason", 999.0),            new Employee("Canson", 1599.1),            new Employee("Jack", 887.9)        };                System.out.println("--------------------");                System.out.println("before sort:");        for(int i = 0 ; i < emps.length ; i++)            System.out.println(emps[i].toString());                Arrays.sort(emps);                System.out.println("after sort:");        for(int i = 0 ; i < emps.length ; i++)            System.out.println(emps[i].toString());            }}


結果:

before sort:
nameJasonweight89
nameAmemleweight100
nameMikeweight72
nameHamweight102
after sort:
nameMikeweight72
nameJasonweight89
nameAmemleweight100
nameHamweight102
--------------------
before sort:
name:Jason salary:999.0 ElyId:502862
name:Canson salary:1599.1 ElyId:716871
name:Jack salary:887.9 ElyId:552929
after sort:
name:Jason salary:999.0 ElyId:502862
name:Jack salary:887.9 ElyId:552929
name:Canson salary:1599.1 ElyId:716871

模板模式和Comparable類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.