顯示具有 Java SE::抽象類別與介面 標籤的文章。 顯示所有文章
顯示具有 Java SE::抽象類別與介面 標籤的文章。 顯示所有文章

2015年9月30日 星期三

Comparator 的用法

Comparator 的用法:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorDemo {
    public static void main(String[] args){
    
        List<Student> studentList = new ArrayList<>(3);
        Comparator<Student> sortName = new StudentSortName();
        Comparator<Student> sortGpa = new StudentSortGpa();
        
        studentList.add(new Student("Thomas Jefferson", 1111, 3.8));
        studentList.add(new Student("John Adams", 2222, 3.9));
        studentList.add(new Student("George Washington", 3333, 3.4));
        
        Collections.sort(studentList, sortName);
        for (Student student : studentList){
            System.out.println(student.getName());
        }
         Collections.sort(studentList, sortGpa);
        for (Student student : studentList){
            System.out.println(student.getGpa());
        }
    }
}

public class Student {
private String name;
    private long id = 0;
    private double gpa = 0;
            
    public Student(String name, long id, double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
    }

    public String getName() {
        return name;
    }

    public double getGpa() {
        return gpa;
    }
}

import java.util.Comparator;

public class StudentSortName implements Comparator<Student> {

    public StudentSortName() {
    }

    @Override
    public int compare(Student o1, Student o2) {
        int result = o1.getName().compareTo(o2.getName());
        if (result != 0){return result;}
        else { return 0;}
    }
}

import java.util.Comparator;

public class StudentSortGpa implements Comparator<Student> {

    public StudentSortGpa() {
    }

    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getGpa() < o2.getGpa()){return 1;}
        else if (o1.getGpa() > o2.getGpa()){ return -1;}
        else {return 0;}
    }
}

Comparable 的用法

Comparable 的用法:
public class ComparableStudent implements Comparable<ComparableStudent>{
    private String name;
    private long id = 0;
    private double gpa = 0.0;

    public ComparableStudent(String name, long id, double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
    }

    public String getName() {
        return name;
    }
        
    @Override
    public int compareTo(ComparableStudent o) {
        int result = this.name.compareTo(o.getName());
        if (result > 0){return 1;}
        else if (result < 0){return -1;}
        else {return 0;}
    }
}

Demo 程式:
import java.util.Set;
import java.util.TreeSet;

public class Demo {
    public static void main(String[] args){
        Set<ComparableStudent> studentList = new TreeSet<>();
        
        studentList.add(new ComparableStudent("Thomas Jefferson", 1111, 3.8));
        studentList.add(new ComparableStudent("John Adams", 2222, 3.9));
        studentList.add(new ComparableStudent("George Washington", 3333, 3.4));
         
         for (ComparableStudent student : studentList){
             System.out.println(student.getName());
         }
    }
}

2015年9月11日 星期五

Java 介面實作與應用

Java 介面宣告方式:
public interface Refuel{
  
    //介面的方法,可視為抽象方法!
    public String doRefuel();
}

修改一下原來的 Sedan 類別
public class Sedan extends Vehicle implements Refuel{

    private char audioClass;
    private double fuelTank;

    Sedan(int wheels, String engineType, double weight, char audio , double fuelTank){
      super(wheels,engineType,weight);
      this.audioClass = audio;
      this.fuelTank = fuelTank;
    }

    //實作抽象方法
     public void ignite(){
       System.out.println("Engine start!");
    }

    //實作介面方的方法
    public String doRefuel(){
      this.fuelTank = 20.00;
      return "Refeuel :" + this.fuelTank;
    }
}

實作 Sedan 類別:
public class Demo {
 public static void main(String[] args){

    Sedan bm = new Sedan(4, "Diesel" , 2231.21 , 'A' , 0.0);
    System.out.println(bm.doRefuel());
    bm.ignite();
 }
}

Java 抽象類別

Java 抽象類別的使用:(hint: 把所有類別重複的東西,描象化成一個 super 類別!)
public abstract class Vehicle{
  
   private int wheels;
   private String engineType;
   private double weight;

   Vehicle(int wheels, String engineType, double weight){
       setWheels(wheels);
       setEngineType(engineType);
       setWeight(weight);
   }

   public abstract void ignite(); //這是抽象方法

   public int getWheels(){
     return this.wheels;
   }

   public String getEngineType(){
     return this.engineType;
   }

   public double getWeight(){
     return this.weight;
   }

   public void setWheels(int wheels){
     this.wheels = wheels;
   }

   public void setEngineType(String engineType){
     this.engineType = engineType;
   }

   public void setWeight(double weight){
     this.weight = weight;
   }
}

利用繼承,才能使用抽象類別:
public class Sedan extends Vehicle{

    private char audioClass;

    Sedan(int wheels, String engineType, double weight, char audio){
      super(wheels,engineType,weight);
      this.audioClass = audio;
    }

    //實作抽象方法
     public void ignite(){
       System.out.println("Engine start!");
    }
}

實作時,不可以實例化抽象類別:
public class Demo {
 public static void main(String[] args){

    //錯誤的範例:
    //Vehicle vo = new Vehicle(4, "Gas" , 1234.32);

    //正確的範例:
    Sedan bm = new Sedan(4, "Diesel" , 2231.21 , 'A');
    bm.ignite();
 }
}