接口与Comparable
在JAVA中,接口并不是类,而是对类的一组需求的描述,如果类想实现某一
特定功能,就可以实现某一接口。比如:如果需要用Arrays中的sort方法对对象
数组进行排序,那对象所属的类必须实现Comparable接口。
接口中的方法自动属于public,接口中不能有实例域也不能实现方法,只是
告知你需要实现哪些方法。
一个类要实现排序服务必须要实现compareTo方法,但是为了确保该类必须实
现了该方法才能保证对它进行排序,而要实现接口,就必须实现它的所有方法,所
以只要该类实现了Comparable接口就能保证能对它排序。
特定功能,就可以实现某一接口。比如:如果需要用Arrays中的sort方法对对象
数组进行排序,那对象所属的类必须实现Comparable接口。
接口中的方法自动属于public,接口中不能有实例域也不能实现方法,只是
告知你需要实现哪些方法。
一个类要实现排序服务必须要实现compareTo方法,但是为了确保该类必须实
现了该方法才能保证对它进行排序,而要实现接口,就必须实现它的所有方法,所
以只要该类实现了Comparable接口就能保证能对它排序。
import java.util.Arrays;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
class Student implements Comparable<Student>{
int id;
Student(int _id){
id=_id;
}
public int compareTo(Student other) {
return id%10-other.id%10;
}
}
public class Demo {
public static void print(String s){
System.out.println(s);
}
public static void main(String []args)
{
Student []students=new Student[]{
new Student(100),new Student(9),new Student(11)};
Arrays.sort(students);
for(Student s:students)
print(s.id+" ");
}
}
如果子类定义的比较语义不一样,应当对于compareTo方法进行检测
if(getClass()!=other.getClass())throw new ClassCastException();
如果需要一种通用的比较规则,可以在超类中提供一个compareTo的方法,并且
将它设置为final
不能创建接口的对象,但是可以声明一个接口的变量,并且该变量必须引用
一个实现了该接口的类对象。
可以使用 instanceof 判断是否实现了某一接口。接口中的方法全部为final
而其中的域全部为 public static final ,接口中可以有静态的常量。
//在上面代码继续添加:
Comparable<Student> com=students[0];
print(com.compareTo(students[1])>0?"YES":"NO");
print((students[0] instanceof Comparable)+"");
接口可以继承:
interface Skill{
public void say();
}
interface HighSkill extends Skill{
public void sing();
}
class People implements HighSkill{
public void say() {
System.out.println("i can say");
}
public void sing() {
System.out.println("i can sing");
}
}
对象克隆
当直接用'='号对对象进行复制时,只是将原始变量与拷贝变量引用同一个对
象当其中一个对象发生变化时,会对其另一个变量也产生影响,如果要创建对象的
一个拷贝,并且在将来可以改变各自的状态,应该使用 clone方法。
象当其中一个对象发生变化时,会对其另一个变量也产生影响,如果要创建对象的
一个拷贝,并且在将来可以改变各自的状态,应该使用 clone方法。
class Apple implements Cloneable
{
int []id={0,1,2};
@Override
public Apple clone() throws CloneNotSupportedException
{
Apple tmp=(Apple)super.clone();
tmp.id=id.clone();
return tmp;
}
}
public class Demo {
public static void print(String s){
System.out.println(s);
}
public static void main(String []args) throws CloneNotSupportedException
{
Apple a=new Apple();
Apple b=a.clone();
Apple c=a;
print("a==b ?\t"+(a==b));
print("a.id==b.id ?\t"+(a.id==b.id));
print("a==c ?\t"+(a==c));
print("a.id==c.id ?\t"+(a.id==c.id));
}
}
运行结果应该是:
a==b ? false
a.id==b.id ? false
a==c ? true
a.id==c.id ? true
可以看出普通的复制只是将它们引用到同一个对象,而重写的clone
则是一个新的对象。
在 Object 类中clone 方法是受保护的,也即是说用户在编码时不能直接调用
它,只有类自己才能clone 自己。
如果需要实现clone功能,应当实现 Clonable 接口,否则会出现一个已检验异
常(checked exception).而 Clonable 接口 是 JAVA 提供的几个标记接口,其中并
没有clone 方法,它只是为了确保类实现某一特定方法,子类的clone方法只是对超
类或者Object类的重写。
则是一个新的对象。
在 Object 类中clone 方法是受保护的,也即是说用户在编码时不能直接调用
它,只有类自己才能clone 自己。
如果需要实现clone功能,应当实现 Clonable 接口,否则会出现一个已检验异
常(checked exception).而 Clonable 接口 是 JAVA 提供的几个标记接口,其中并
没有clone 方法,它只是为了确保类实现某一特定方法,子类的clone方法只是对超
类或者Object类的重写。
如果要深度拷贝,必然要对域中每个元素进行深度拷贝。