<CoreJava> 6.1 接口

1、接口不是类;只是对类的一组描述。一个类满足了这一描述就可以使用接口中所提供的方法。例如,Arrays类中的sort方法可以对对象数组进行排序,前提是要满足Comlarable接口。下面是Comparable接口的代码:
public interface Comparable {
    int compareTo(Object other);
}
即所有实现Comparable接口的类都可以使用compareTo方法。

2、接口中的所有方法都是public,所以声明方法时不需要使用public标识。

3、接口中绝对不能含有实例域;也不能在接口中实现方法。实例域的提供和方法的实现是由实现接口的类完成的。可以将接口看成是提供方法的容器。

4、假设要让Arrays类的sort方法对Employee对象数组进行排序,Employee类要实现Cpmrarable接口。

一个类要实现一个方法必须满足两个条件:
1)将类声明为实现给定的接口。(声明时使用implements关键字)
class Employee implements Comparable

2)定义接口中的方法。
public int compareTo(Object otherObject) {
    Employee other = (Employee) otherObject;
    if (salary < other.salary) return -1;
    if (salary > other.salaey) return 1;
    return 0;
}
警告:接口中的方法全部自动设为public所以不用声明,但是在实现接口中的方法时一定要将其声明为public。

5、源代码:例6-1,实现雇员数组排序的全部代码。
EmployeeSortTest.java:
  1. package com.vincent.corejava.employeesorttest;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5.   
  6. public class EmployeeSortTest {  
  7.     public static void main(String[] args) {  
  8.         Employee[] staff = new Employee[3];  
  9.           
  10.         staff[0] = new Employee("Harry"35000);  
  11.         staff[1] = new Employee("Carl"75000);  
  12.         staff[2] = new Employee("Tony"38000);  
  13.           
  14.         Arrays.sort(staff);  
  15.           
  16.         for (Employee e : staff)  
  17.             System.out.println("name: " + e.getName() + ", salary: " + e.getSalary());  
  18.     }  
  19.   
  20. }  

Employee.java:
  1. package com.vincent.corejava.employeesorttest;  
  2.   
  3. class Employee implements Comparable<Employee> {  
  4.   
  5.     private String name;  
  6.     private double salary;  
  7.   
  8.     public Employee(String aName, double aSalary) {  
  9.         name = aName;  
  10.         salary = aSalary;  
  11.     }  
  12.       
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.       
  17.     public double getSalary() {  
  18.         return salary;  
  19.     }  
  20.       
  21.     public void raiseSalary(double byPercent) {  
  22.         double raise = salary * byPercent / 100;  
  23.         salary += raise;  
  24.     }  
  25.   
  26.     @Override  
  27.     public int compareTo(Employee other) {  
  28.         if (salary < other.salary) return -1;  
  29.         if (salary > other.salary) return 1;  
  30.         return 0;  
  31.     }  
  32.   
  33. }  

运行结果:
name: Harry, salary: 35000.0
name: Tony, salary: 38000.0
name: Carl, salary: 75000.0

6.1.1 接口特性 
1、接口不是类,不能用new运算符实例化一个接口;

2、不能构造接口对象,但是可以声明接口变量:
Comparable x;

3、接口变量必须引用实现该接口的类对象:
x = new Employee(...);

4、可以使用instance检查一个对象是否实现了某个特定的接口:
if (anObject instanceof Comparable) {....}

5、接口可以使用集成关系来扩展自身:
public interface Moveable {
    void move(double x, double y);
}
然后,以此为基础进行扩展:
public interface Powered extends Moveable {
    double milesPerGallon;
}

6、接口中不能有实例域或静态方法,但可以有常量:
public interface Powered extends Moveable {
    double milesPerGallon();
    double SPEED_LTMTL = 95;
}
接口中的域被自动设为public sratic final.

6.1.2 接口与抽象类
一个类只能以集成类的方式扩展一次,但是可以使用接口来获取更多的扩展。接口是为了弥补Java没有多集成带来的功能上的缺失同时也解决了多集成造成语言复杂效率低下的弊端。
posted @ 2013-02-28 18:56  vincent_hv  阅读(168)  评论(0编辑  收藏  举报