代码:package aa11222223;(接口cloneable已经另外定义了{})public class Student implements Cloneable{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public Object clone(){
Object o=null;
try{
o=(Student)super.clone();
}catch(CloneNotSupportedException ex){
System.out.println(ex.toString());
}return o;

}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=new Student("zhangsan",23);
Student s2=(Student)s1.clone();
System.out.println("s1'name:"+s1.name+" s1's age:"+s1.age);
System.out.println("s2'name:"+s2.name+" s2's age:"+s2.age);
s2.name="lisi";
s2.age=24;
System.out.println("s1'name:"+s1.name+" s1's age:"+s1.age);
System.out.println("s2'name:"+s2.name+" s2's age:"+s2.age); }}
运行时抛出错误java.lang.CloneNotSupportedException: aa11222223.Student
Exception in thread "main" java.lang.NullPointerException
at aa11222223.Student.main(Student.java:28)
为什么?
扩展:如果这个类只是父类,还有子类继承他,子类的clone方法该如何写?

解决方案 »

  1.   

    clone 方法 throw CloneNotSupportedException子类加一个super.clone();
      

  2.   

    clone()方法是Object类中定义的,但是这并不是说任何类都可以clone,要想clone必须要实现Cloneable接口才可以。
    你已经把cloneable另外定义了,就不是jdk类库里面的Cloneable了!
      

  3.   

    package aa11222223;public interface Cloneable {}
    这就是我另外定义的
      

  4.   

    Cloneable必须是java.lang包里面的才行。
    虽然你的和类库里面的长得一模一样!
      

  5.   

    那意思是我不用再声明接口,只需要import java.lang.*就行了?
      

  6.   

    JVM会自动加载java.lang里面的类,所以,你就不用多此一举了!
      

  7.   

    扩展问一下,如果我有个GeometricObject的抽象类
    public abstract class GeometricObject implements Cloneable{

    public Object clone() throws CloneNotSupportedException{
    GeometricObject o=null;
    try{o=(GeometricObject)super.clone();
    }
    catch(CloneNotSupportedException ex){
    System.out.println(ex.toString());
    }
    return o;
    }
    public abstract double getArea(); public abstract double getPerimeter();
    }
    然后子类:public class Octagon extends GeometricObject implements Cloneable{
    private double bound;
    Octagon(){
    bound=1;
    }
    Octagon(double bound){
    this.bound=bound;
    }
    public double getArea()
    {return (2+4/Math.sqrt(2))*bound*bound;
    }
    public double getPerimeter(){
    return 8*bound;
    }
        public Octagon clone() throws CloneNotSupportedException  {  
         return (Octagon)super.clone();} 
        }

        现在想在main中,用子类的Clone方法,该如何做呢?
      

  8.   

    现在想在main中,用子类的Clone方法,该如何做呢? 
    测试代码如下
    package com.yhz.clone;public abstract class GeometricObject implements Cloneable { public Object clone() throws CloneNotSupportedException {
    GeometricObject o = null;
    try {
    o = (GeometricObject) super.clone();
    } catch (CloneNotSupportedException ex) {
    System.out.println(ex.toString());
    }
    return o;
    } public abstract double getArea(); public abstract double getPerimeter();

    public static void main(String[] args) throws CloneNotSupportedException {
    Octagon o = new Octagon();
    Octagon c = (Octagon)o.clone();
    System.out.println(c.getArea());
    c.setBound(2);
    System.out.println(o.getArea());
    System.out.println(c.getArea());
    }
    }class Octagon extends GeometricObject implements Cloneable {
    private double bound; Octagon() {
    bound = 1;
    } Octagon(double bound) {
    this.bound = bound;
    }

    public void setBound(double bound){
    this.bound = bound;
    } public double getArea() {
    return (2 + 4 / Math.sqrt(2)) * bound * bound;
    } public double getPerimeter() {
    return 8 * bound;
    } public Octagon clone() throws CloneNotSupportedException {
    return (Octagon) super.clone();
    }
    }package com.yhz.clone;public abstract class GeometricObject implements Cloneable { public Object clone() throws CloneNotSupportedException {
    GeometricObject o = null;
    try {
    o = (GeometricObject) super.clone();
    } catch (CloneNotSupportedException ex) {
    System.out.println(ex.toString());
    }
    return o;
    } public abstract double getArea(); public abstract double getPerimeter();

    public static void main(String[] args) throws CloneNotSupportedException {
    Octagon o = new Octagon();
    Octagon c = (Octagon)o.clone();
    System.out.println(c.getArea());
    c.setBound(2);
    System.out.println(o.getArea());
    System.out.println(c.getArea());
    }
    }class Octagon extends GeometricObject implements Cloneable {
    private double bound; Octagon() {
    bound = 1;
    } Octagon(double bound) {
    this.bound = bound;
    }

    public void setBound(double bound){
    this.bound = bound;
    } public double getArea() {
    return (2 + 4 / Math.sqrt(2)) * bound * bound;
    } public double getPerimeter() {
    return 8 * bound;
    } public Octagon clone() throws CloneNotSupportedException {
    return (Octagon) super.clone();
    }
    }