在该方法中使用了native关键字(简单地讲,一个native Method就是一个java调用非java代码的接口。一个Native Method是这样一个java的方法:该方法的实现由非java语言实现,比如C。这个特征并非java所特有,很多其它的编程语言都有这一机制,比如在C++中,你可以用extern "C"告知C++编译器去调用一个C的函数.)而且没有方法实现,如果源类没有继承自Cloneable接口,那么编译器会抛出CloneNotSupportedException异常。
我们再看Student.java中用到的teacher.java:
package clonedemo;
public class teacher implements Cloneable...{
private int age ;
private String name ;
teacher(String name,int age)...{
this.age = age ;
this.name = name ;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public Object clone()...{
Object obj = null ;
try ...{
obj = super.clone() ;
} catch (CloneNotSupportedException e) ...{
// TODO: handle exception
e.printStackTrace() ;
}