6.1接口
例,排序算法需要类实现比较接口。
public interface Comparable{int compareTo(Object other);}
泛型
public interface Comparable<T>
{int compareTo(T other);//parameter has type T
}
例Employee类实现接口
int compareTo(Employee other);
接口里的方法默认是Public的。
注意接口是没有实例的。
定义:
class Employee implements Comparable
public int compareTo(Object otherObject){Employee other=(Employee)otherObject;return Double.compare(salary,other.salary);
}
或
class Employee implements Comparable<Employee>{public int compareTo(Employee other){return Double.compare(salary,other.salary);}
}