1.接口的定义:
In the Java programming language, an interface is not a class but a set of requirements for classes that
want to conform the interface.
说明: 1) Interface
不是class,虽然interface具有class的一些特点,例如能够继承,能够定义相同类型的变量,而且和C++的abstract
class非常像,但Java
的interface和class在本质上已经有所区别!
2) Interface 是一组要求。类的接口是类的调用者和被调用者之间的一个契约,这个契约定义了interface实现者所要提供的功能和功能提供的方式,也定义了
interface调用者获取 interface实现者服务的方式。
为了让某个class实现一个接口,只需要以下两个步骤:
1) 声明你所定义的类将实现某个接口;
2) 在你的类中实现接口中所有的方法;
下面的Employee类实现了compareTo接口,那么就可以使用Array的sort方法来对Emplyee对象形成的数组进行排序;
1 import java.util.Arrays;
2
3
4 public class EmployeeSortTest
5 {
6 public static void main(String[] args)
7 {
8 Employee[] staff = new Employee[3];
9
10 staff[0] = new Employee("Harry Hacker",35000);
11 staff[1] = new Employee("Carl Cracker",75000);
12 staff[2] = new Employee("Tony Tester",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
21 class Employee implements Comparable<Employee>
22 {
23 public Employee(String n,double s)
24 {
25 name = n;
26 salary = s;
27 }
28
29 public String getName()
30 {
31 return name;
32 }
33
34 public double getSalary()
35 {
36 return salary;
37 }
38
39 public void raiseSalary(double byPercent)
40 {
41 double raise = salary * byPercent / 100;
42 salary += raise;
43 }
44
45 /**
46 * Compares employees by salary
47 */
48 public int compareTo(Employee other)
49 {
50 if(salary < other.salary) return -1;
51 if(salary > other.salary) return 1;
52 return 0;
53 }
54
55 private String name;
56 private double salary;
57 }
接口的一些特性:
1.接口不是类,你无法new出一个接口实例:
2.虽然无法构建出接口实例,但是可以定义接口变量; 并且接口变量只能指向一个实现该接口的类的对象;
Comparable x;
x = new Employee(...);
3.就像使用instanceof判断一个对象是不是一个类的实例一样,你也可以使用instanceof判断一个类的对象是否实现了某个接口。
if(anObject instanceof Comparable) {}
4.就像类的使用一样,接口也可以用来被继承;
5.Java中的类只能继承一个父类,但却可以实现多个接口!这个特性为定义一个类的行为提供的很大的方便。
接口和抽象类:
为什么不使用抽象类来代替接口的概念呢?--Java中不存在多重继承!Java中用接口来实现了C++中复杂的多继承功能!
2. 对象克隆(Object Cloning):
对任何一个对象你需要认清下面几点:
a). 默认的clone函数是否已经足够优秀!
b). Clone一个基类对象时要Clone其成员的每个mutable部分;
c). Clone是否根本无法实现;
对象型变量实际存储的是对象实例的地址,而不是对象实例本身,这是Java设计的一个遗憾!
1 import java.util.*;
2
3 public class CloneTest
4 {
5 public static void main(String[] args)
6 {
7 try
8 {
9 MyEmployee original = new MyEmployee("John Q. Public",50000);
10 original.setHireDay(2000, 1, 1);
11 MyEmployee copy = original.clone();
12 copy.raiseSalary(10);
13 copy.setHireDay(2002, 12, 31);
14 System.out.println("original="+original);
15 System.out.println("Copy="+copy);
16 }
17 catch(CloneNotSupportedException e)
18 {
19 e.printStackTrace();
20 }
21 }
22 }
23
24 class MyEmployee implements Cloneable
25 {
26 public MyEmployee(String n,double s)
27 {
28 name = n;
29 salary = s;
30 hireDay = new Date();
31 }
32
33 public MyEmployee clone() throws CloneNotSupportedException
34 {
35 MyEmployee cloned = (MyEmployee)super.clone();
36
37 cloned.hireDay = (Date)hireDay.clone();
38
39 return cloned;
40 }
41
42 public void setHireDay(int year,int month,int day)
43 {
44 Date newHireDay = new GregorianCalendar(year,month-1,day).getTime();
45 hireDay.setTime(newHireDay.getTime());
46 }
47
48 public void raiseSalary(double byPercent)
49 {
50 double raise = salary * byPercent / 100;
51 salary += raise;
52 }
53
54 public String toString()
55 {
56 return "MyEmployee[name="+name+",salary="+salary+"hireDay="+hireDay+"]";
57 }
58
59 private String name;
60 private double salary;
61 private Date hireDay;
62 }
结果如下:
original=MyEmployee[name=John Q. Public,salary=50000.0hireDay=Sat Jan 01 00:00:00 CST 2000]
Copy=MyEmployee[name=John Q. Public,salary=55000.0hireDay=Tue Dec 31 00:00:00 CST 2002]
3.接口和回调:
一个在编程中常用的模型称为callback(模型),当一个事件发生时要制定处理动作。比如当按钮被按下后执行的特定动作,或者选择了一个
菜单选项之后执行的特定动作!
Java语言利用传递对象来实现这一点,但C++使用传递函数指针来实现这一点的!
1 /**
2 @version 1.00 2000-04-13
3 @author Cay Horstmann
4 */
5
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.*;
9 import javax.swing.*;
10 import javax.swing.Timer;
11 // to resolve conflict with java.util.Timer
12
13 public class TimerTest
14 {
15 public static void main(String[] args)
16 {
17 ActionListener listener = new TimePrinter();
18
19 // construct a timer that calls the listener
20 // once every 10 seconds
21 Timer t = new Timer(10000, listener);
22 t.start();
23
24 JOptionPane.showMessageDialog(null, "Quit program?");
25 System.exit(0);
26 }
27 }
28
29 class TimePrinter implements ActionListener
30 {
31 public void actionPerformed(ActionEvent event)
32 {
33 Date now = new Date();
34 System.out.println("At the tone, the time is " + now);
35 Toolkit.getDefaultToolkit().beep();
36 }
37 }
Java面向对象程序设计--接口和内部类