代码: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方法该如何写?
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方法该如何写?
你已经把cloneable另外定义了,就不是jdk类库里面的Cloneable了!
这就是我另外定义的
虽然你的和类库里面的长得一模一样!
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方法,该如何做呢?
测试代码如下
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();
}
}