一、接口
接口是一组对类的需求描述,也即可以理解为接口只是定义了一堆方法,但其自己不实现这些方法,具体如何实现交给需要实现接口的类去做。
定义接口
public interface Comparable<T>
{
int compareTo(T other);
}
接口中的方法默认是public的。接口中不能有实例域和方法实现,但接口中可以有常量,并且默认是public static final的。
将类声明为实现了某个接口,需使用implements关键字。
public class Employee implements Comparable<Employee>
{
public int compareTo(Employee other);
}
类实现了某个接口,在类里面,该方法需标明关键字public。
接口本身不是类,所以不能实例化接口,但可以声明接口变量,接口变量也可以引用实现了该接口的类对象。
Comparable tempC;
tempC = new Employee("zhangs",100);
与使用instanceof检查一个对象是否属于某一个类一样,也可以使用instanceof方法检查一个对象是否实现了某个接口
if(tempEmp instanceof Comparable)
{
//......................
}
类可以继承,接口也可以类似
public interface compTest extends Comparable
{
//..................
}
一个类只能有一个超类,但可以实现多个接口。
import java.lang.reflect.*;
import java.util.*;
public class Test28
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("zhangs",200);
staff[1] = new Employee("lisi",100);
staff[2] = new Employee("wange",300);
Arrays.sort(staff);
for(Employee te:staff)
System.out.println(te.toString());
}
}
class Employee implements Comparable<Employee>
{
public Employee(String name,int id)
{
this.name = name;
this.id = id;
nextId++;
}
public int compareTo(Employee other)
{
if(this.id > other.id)
return 1;
if(this.id < other.id)
return -1;
return 0;
}
public String toString()
{
return getClass().getName()
+"[name="+name+" id="+id+"]";
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public float getSalary()
{
return salary;
}
public void setSalary(float salary)
{
this.salary = salary;
}
public static int getNextId()
{
return nextId ;
}
private String name;
private int id;
private static int nextId = 0;
private float salary ;
}
二、接口与抽象类
接口与抽象类都不可以实例化。
但抽象类是类,而接口不是类。
抽象类使用关键字abstract修饰,接口使用interface。
抽象类中可以有实例域和具体方法,接口中不能。
一个类只能有一个父类,而可以实现多个接口。
像上面的Comparable接口,如果设置为抽象类,方法compareTo设置为抽象方法,那对于有些已经有一个父类的类,要再实现compareTo方法就不可能了,因为一个类不能有多个父类,但为接口就可以。
三、对象克隆
默认的克隆操作是“浅拷贝”,即该种拷贝并未拷贝包含在对象中的内部对象。
如果需要“深拷贝”,即拷贝一个副本的形式,就需要实现接口Cloneable。实现clone方法。
在Object类中clone方法是声明为Protected的,所以只能克隆Object对象本身。
接口Cloneable本身是没有方法的,它的存在只是为了做instanceof操作,即判断是否实现了该接口而已,这种接口被称为标记接口。
只要在clone方法中包含没有实现Cloneable接口的对象,就会抛出CloneNotSupportException。
import java.util.* ;
public class Test29
{
public static void main(String[] args) throws CloneNotSupportedException
{
Employee original = new Employee("zhangs");
original.setHireDay(2013,12,13);
System.out.println(original.toString());
Employee copy = original.clone();
copy.setHireDay(2012,12,12);
System.out.println(original.toString());
System.out.println(copy.toString());
}
}
class Employee implements Cloneable
{
public Employee(String name)
{
this.name=name;
}
public Employee clone() throws CloneNotSupportedException
{
Employee emp = (Employee)super.clone();
emp.hireDay = (Date)hireDay.clone();
return emp;
}
public void setHireDay(int year,int month,int day)
{
hireDay = new GregorianCalendar(year,month-1,day).getTime();
}
public String toString()
{
return getClass().getName()
+"[name="+name+" hireDay="+hireDay+"]";
}
private String name;
private Date hireDay;
}
四、接口与回调
所谓回调就是在某个特定事件触发的时候应该采取的动作。
如javax.swing包中的Timer类,指定时间间隔,触发一个或者多个动作。
Timer类的构造方法 Timer(int delay,ActionListener listener),其中delay表示延迟时间,ActionListener是接口,表示需要采取的动作。
public interface ActionListener
{
void actionPerformed(ActionEvent event);
}
到达指定间隔,就会执行actionPerformed方法。ActionEvent中包含事件信息,可以先不考虑。
此处我们可以传递一个实现了ActionListener接口的对象。
import java.util.* ;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
public class Test30
{
public static void main(String[] args)
{
ActionListener act = new TimerPrint();
Timer t = new Timer(10000,act);
t.start();
JOptionPane.showMessageDialog(null,"Quit Programe?");
System.exit(0);
}
}
class TimerPrint implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Date now = new Date();
System.out.println("now the time is "+now);
Toolkit.getDefaultToolkit().beep();
}
}
执行结果: