当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

成都信息工程大学(成都信工学院):《Java编程语言 The Java Programming Language》课程教学资源(PPT课件讲稿)Cloning Objects

资源类别:文库,文档格式:PPT,文档页数:23,文件大小:1.32MB,团购合买
在程序设计中通常可以使用以下方式实现克 隆对象: (1) 使用拷贝构造函数(Copy Constructor) (2) 使用拷贝工厂方法 (Copy Factory Method) (3) 使用Java语言提供的对象克隆机制
点击下载完整版文档(PPT)

Cloning Objects 复制对象指创建一个新的与指定对象具有相 同状态的对象副本(Copy),又称为克隆对象 在程序设计中通常可以使用以下方式实现克 隆对象: (1)使用拷贝构造函数( Copy Constructor) (2)使用拷贝工厂方法( Copy Factory Method (3)使用Java语言提供的对象克隆机制

Cloning Objects • 复制对象指创建一个新的与指定对象具有相 同状态的对象副本(Copy),又称为克隆对象 • 在程序设计中通常可以使用以下方式实现克 隆对象: (1) 使用拷贝构造函数 (Copy Constructor) (2) 使用拷贝工厂方法 (Copy Factory Method) (3) 使用Java语言提供的对象克隆机制

class Sheep private string name; private float weight; private string color; public Sheep(string name, float weight, String color)t this name=name; this, weight=weight this color=color; public Sheep(sheep other) this. name=other name this weight=- other weight;拷贝构造函数 this color=other color: ∥…它方法 Sheep sheep= new Sheep(“ Xiaobai”,45,“ White”); Sheep sheep=new Sheep(sheep);

Copy Constructor • 拷贝构造函数是指以类自身的引用类型作为 参数,在构造函数中将作为参数的对象的状 态(对象的域)赋值给新建的对象 • 在拷贝构造函数中可以直接访问作为参数的 对象的任何域 class Sheep{ 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 Sheep(Sheep other){ this.name=other.name; this.weight=other.weight; this.color=other.color; } // ……其它方法 } 拷贝构造函数 Sheep sheepA=new Sheep(“XiaoBai”,45, “White”); Sheep sheepB=new Sheep(sheepA);

class ca private String name; private String color public String getNameoi return name; y public void setName(String newName)( name=newName public String getcoloro return color; 3 public void set Color(String new Color) color=new Color 返 public static Cat copy Cat(Cat other) Cat cat=new Cato; cat setName(othergetName(); 拷贝工厂方法 cat setColor(other getcoloro; Cat catA- new Cato; catA setName(MiMi); catA. set Color(“ Black”); Cat catB=Cat. copy Cat(catA);

Copy Factory Method • 拷贝工厂方法与拷贝构造函数类似,其区别 是拷贝工厂方法是一个静态方法,该方法返 回所创建的对象副本 • 拷贝工厂方法中适用于JavaBean风格的类的 对象复制 class Cat{ private String name; private String color; public String getName() { return name; } public void setName(String newName) { name=newName; } public String getColor() { return color; } public void setColor(String newColor) { color=newColor; } public static Cat copyCat(Cat other){ Cat cat=new Cat(); cat.setName(other.getName()); cat.setColor(other.getColor()); } } Cat catA=new Cat(); catA.setName(“MiMi”); catA.setColor(“Black”); Cat catB=Cat.copyCat(catA); 拷贝工厂方法

class SuperClass( private int a; protected int b Super Class(inti, int t a=1; b class SubClass extends SuperClass( private int b SubClass(inti, int j, int k)i super(i,j b=k SubClass(subClass sub) super(sub a, sub b) 错误 b=sub. b:

Shortcoming Of Copy Constructor • 拷贝构造函数的缺点: (1) 必须针对对象的每个域进行赋值操作 (2) 无法访问父类或祖先类中私有的或被隐 藏的受保护的域 • 拷贝构造函数或拷贝工厂方法只适用于简单 的类的对象的复制 class SuperClass{ private int a; protected int b; SuperClass(int i, int j){ a=i; b=j; } } class SubClass extends SuperClass{ private int b; SubClass(int i, int j, int k){ super(i, j); b=k; } SubClass(SubClass sub){ super(sub.a, sub.b); b=sub.b; } } 错误

Java Object Cloning Mylechaniso Java语言在语言级提供了一种对象 复制机制,类的对象要支持复制, 必须使用接口 java. lang Cloneable和 类 Objectl的 Iclone0方法 EJect

Java Object Cloning Mechanism • Java语言在语言级提供了一种对象 复制机制,类的对象要支持复制, 必须使用接口java.lang.Cloneable和 类Object的clone()方法

java, lang Cloneable interface °接口 java. lang Cloneable是一个标记接口 WM将对实现了 java. lang Cloneable接口并且 提供了 publick的lone)方法的类的对象提住 复制支持 ess类名> implements Cloneable 999 public Object cloneD oo.)

java.lang.Cloneable Interface • 接口java.lang.Cloneable是一个标记接口 • JVM将对实现了java.lang.Cloneable接口并且 提供了public的clone()方法的类的对象提供 复制支持 class implements Cloneable{ // …… public Object clone() { … } }

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();

public class IntegerStack implements Cloneable( private int[ buffer; private int top public IntegerStack(int maxSize b 29 buffer ou bb first 考 top: 1 publ buffer ret second top: 1 publ Ion return super clone; 国■加 IntegerStack first-=new IntegerStack (2); first push (2); first push(9); IntegerStack second=(IntegerStack)first clone(;

Shallow Clone • 方法Object.clone()只创建当前对象的影子拷 贝,把这个过程称为影子复制 • 影子复制是在复制过程中只把基本数据类型 的引用类型的值复制到新的对象中 • 源对象与其影子拷贝的基本数据类型的域具 有相同的值,当引用类型的域且共同引用相 同的对象 public class IntegerStack implements Cloneable{ private int[] buffer; private int top; public IntegerStack(int maxSize){ buffer=new int[maxSize]; top=-1; } public void push(int val){ buffer[++top]=val; } public int pop(){ return buffer[top--]; } public Object clone() throws CloneNotSupportedException{ return super.clone(); } } IntegerStack first=new IntegerStack(2); first.push(2); first.push(9); IntegerStack second=(IntegerStack)first.clone(); buffer top: 1 buffer top: 1 first second 2 9

public class IntegerStackimplements Cloneable private private 29 public l buffer buffer op first 29 top: 1 public buffer buffer second public top return public Object cloned t try IntegerStack copy=(IntegerStacksuper clone; copy buffer=(intD)bufferclone; return copy; Icatch(CloneNotSupported Exceptione) throw new InternalErroro; j

Deep Clone • 深度复制是指在对象复制过程中除了所有的 基本数据类型的域将被复制,所有的引用类 型的域所引用的对象也将被复制 • 数组对象和类库绝大部分的集合对象都支持 复制机制 • 如果对象包含不可变对象的域,如String类 对象和Wrapper类对象,则无需复制这些域 public class IntegerStack implements Cloneable{ private int[] buffer; private int top; public IntegerStack(int maxSize){ buffer=new int[maxSize]; top=-1; } public void push(int val){ buffer[++top]=val; } public int pop(){ return buffer[top--]; } public Object clone() { try{ IntegerStack copy=(IntegerStack)super.clone(); copy.buffer=(int[])buffer.clone(); return copy; }catch(CloneNotSupportedException e) { throw new InternalError(); } } } buffer top: 1 buffer top: 1 first second 2 9 2 9

Object serialization 对象序列化 Serialization)是指将对象以字节 流的形式在网上传输,如实现远程方法调用 或保存到文件、数据库中 对象反序列化 serialization)是指从字节流 中重构对象 类的对象要支持序列化和反序列化必须使用 接口 java.io. Serializable和读写对象字节流的 类 ObjectInputstream和 ObjectOutputStream

Object Serialization • 对象序列化(Serialization)是指将对象以字节 流的形式在网上传输,如实现远程方法调用 或保存到文件、数据库中 • 对象反序列化(Deserialization)是指从字节流 中重构对象 • 类的对象要支持序列化和反序列化必须使用 接口java.io.Serializable和读写对象字节流的 类ObjectInputStream和ObjectOutputStream

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共23页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有