正在加载图片...
class Sheep implements Cloneable( private string name; private float weight private string color, public Sheep(string name, float weight, String color) this name=name: this weight-weight; this color=color; public Object cloned return superclone; 3 catch(CloneNotSupportedExceptione)( 调 throw new InternalErroro Sheep sheep= new Sheep(“ XiaoBai”,45,“ White”); Sheep sheep b=sheep cloned;Object.clone() Method • 类Object的clone()方法如下: protected Object clone() throws CloneNotSupportedExdeption • 实现了java.lang.Cloneable接口的类的对象调 用Object.clone()方法将返回当前对象的一个 影子拷贝(Shallow Copy),这个拷贝中所有 基本数据类型的域与源对象相同 class Sheep implements Cloneable{ private String name; private float weight; private String color; public Sheep(String name,float weight,String color){ this.name=name; this.weight=weight; this.color=color; } public Object clone(){ try{ return super.clone(); } catch(CloneNotSupportedException e) { throw new InternalError(); } } } Sheep sheepA=new Sheep(“XiaoBai”,45, “White”); Sheep sheepB=sheepA.clone();
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有