Comparable:
该接口中的只有一个compareTo(T o)方法:
public interface Comparable<T> {
public int compareTo(T o);
}
继承接口后的方法:
public int compareTo(SubOctagon o) {
return (int) (getArea()-o.getArea());
}
但其实写的时候,如果要使用compareTo方法的话,可以不用继承comparable接口
Cloneable:
lone方法返回与当前对象的一个副本对象。可以通过操作副本对象而不影响当前对象。
使用clone方法需要实现Cloneable接口。并重写Object方法中的clone方法。
需要注意的是在clone在Object中是project修饰符。因为所有类都是Object的子类,所以如果不实现clone方法,在类中可以直接使用父类的clone方法,但是其对象在别的类中不能调用clone方法。所以必须重写clone方法。
如果不实现Cloneable接口,只重写clone方法,调用则会抛出异常。
浅克隆,深克隆
浅拷贝:在填充新对象域的时候,进行简单的字段赋值。
深拷贝:按照惯例,此方法返回的对象应该独立于该对象(正被复制的对象)。要获得此独立性,在 super.clone 返回对象之前,有必要对该对象的一个或多个字段进行修改。这通常意味着要复制包含正在被复制对象的内部“深层结构”的所有可变对象,并使用对副本的引用替换对这些对象的引用。如果一个类只包含基本字段或对不变对象的引用,那么通常不需要修改 super.clone 返回的对象中的字段。(简单来说,就是将该对象内部的对象也克隆一份,而不是简单的引用赋值)
浅克隆:
classHead {
String s;voidset(String s1) {
s=s1;
}
}public class Person implementsCloneable{
Head head;
Person(Head head) {this.head =head;
}protected Object clone() throwsCloneNotSupportedException{return super.clone();
}public static void main(String[] args) throwsCloneNotSupportedException{
Person p= new Person(newHead());
Person p1=(Person)p.clone();
System.out.println("p == p1 " + (p ==p1));
System.out.println("p.head == p1.head " + (p.head ==p1.head));
}
}
输出结果:
p == p1 false
p.head == p1.head true
结果分析:
可以从结果过中看到,克隆创建了一个新的Person对象,但是p.head与p1.head仍然指向同一对象,也就是说p与p1仍然存在联系,这是我们不想看到的。
深拷贝:
class Head implementsCloneable{
String s;voidset(String s1) {
s=s1;
}protected Object clone() throwsCloneNotSupportedException{return super.clone();
}
}public class Person implementsCloneable{
Head head;
Person(Head head) {this.head =head;
}protected Object clone() throwsCloneNotSupportedException{
Person p= (Person)super.clone();
p.head=(Head)head.clone();returnp;
}public static void main(String[] args) throwsCloneNotSupportedException{
Person p= new Person(newHead());
Person p1=(Person)p.clone();
System.out.println("p == p1 " + (p ==p1));
System.out.println("p.head == p1.head " + (p.head ==p1.head));
}
}
输出结果:
p == p1 false
p.head == p1.head false
结果分析:要实现深层拷贝要将对象内部的对象拷贝。
class SubOctagon extends GeometricObject implements /*java.lang.Comparable<SubOctagon>,*/Cloneable{
private double side;
public SubOctagon(double side) {
super();
this.side = side;
}
public int compareTo(SubOctagon o) {
return (int) (getArea()-o.getArea());
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getArea(){
return (2+4*1.0/22)*side*side;
}
public double getPerimeter(){
return side*8;
}
//使用clone方法创建一个新的正八边形
protected SubOctagon clone() throws CloneNotSupportedException {
return (SubOctagon) super.clone();
}
}
public class Octagon01 {
public static void main(String[] args) throws CloneNotSupportedException{
SubOctagon sb=new SubOctagon(3);
System.out.println(sb.getArea());
System.out.println(sb.getPerimeter());
SubOctagon bs=sb.clone();
System.out.println(sb.compareTo(bs));
}
}
protected sub clone() throws CloneNotSupportedException{
return (sub) super.clone();
}
并且
public static void main(String[] args) throws CloneNotSupportedException
也必须写,不然会显示
Unhandled exception: java.lang.CloneNotSupportedException