向量的原型
用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。
浅克隆
深克隆
代码:
Java 浅克隆: 2.1 Attachment.java package qiankelong; public class Attachment { public void download() { System.out.println("复制向量"); } } 2.2 Client.java package qiankelong; public class Client { public static void main(String a[]) { Vector vector,copyVector; vector=new Vector(); int[] arr= {2,8,3,6}; vector.setArray(arr); System.out.println("浅克隆示例:"); copyVector=(Vector)vector.clone(); System.out.print("vector:"); vector.display(); System.out.print("copyVector:"); copyVector.display(); System.out.println("vector==copyVector?"); System.out.println(vector==copyVector); System.out.println("vector.getAttachment==copyVector.getAttachment?"); System.out.println(vector.getAttachment()==copyVector.getAttachment()); } } 2.3 Vector.java package qiankelong; import java.util.Arrays; public class Vector implements Cloneable { private int[] array; private Attachment attachment=null; public Vector() { this.attachment=new Attachment(); } public Object clone() { Vector clone=null; try { clone=(Vector)super.clone(); } catch(CloneNotSupportedException e) { System.out.println("Clone failure!"); } return clone; } public Attachment getAttachment() { return this.attachment; } public void display() { System.out.println(Arrays.toString(array)); } public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } }
深克隆 2.4 Attachment.java package shenkelong; import java.io.*; public class Attachment implements Serializable { public void download() { System.out.println("复制向量"); } } 2.5 Vector.java package shenkelong; import java.io.*; import java.util.*; public class Vector implements Serializable{ private int[] array; private Attachment attachment=null; public Vector() { this.attachment=new Attachment(); } public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException { //将对象写入流中 ByteArrayOutputStream bao=new ByteArrayOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(bao); oos.writeObject(this); //将对象从流中取出 ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray()); ObjectInputStream ois=new ObjectInputStream(bis); return(ois.readObject()); } public Attachment getAttachment() { return this.attachment; } public void display() { System.out.println(Arrays.toString(array)); } public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } 2.6 Client.java package shenkelong; public class Client{ public static void main(String a[]) { Vector vector,copyVector=null; vector=new Vector(); int[] arr= {2,8,3,6}; vector.setArray(arr); System.out.println("深克隆示例:"); try{ copyVector=(Vector)vector.deepClone(); } catch(Exception e) { e.printStackTrace(); } System.out.print("vector:"); vector.display(); System.out.print("copyVector:"); copyVector.display(); System.out.println("vector==copyVector?"); System.out.println(vector==copyVector); System.out.println("vector.getAttachment==copyVector.getAttachment?"); System.out.println(vector.getAttachment()==copyVector.getAttachment()); } }
C++ #include<cmath> #include<iostream> using namespace std; class Vector{ private: double *array =new double[4]; double length; public: Vector(double arr[4]) { this->array[0] = arr[0]; this->array[1] = arr[1]; this->array[2] = arr[2]; this->array[3] = arr[3]; this->length = sqrt(((arr[0] - arr[1])*(arr[0] - arr[1])) + ((arr[2] - arr[3])*(arr[2] - arr[3]))); } ~Vector() { delete[]array; this->length = 0; } Vector* clone() { return new Vector(*this); } Vector(const Vector& vector) { //浅克隆 this->array = vector.array; this->length = vector.length; } void show() { cout << "向量长度:" << this->length << endl; } }; int main() { double s[4] = { 1, 2, 3, 4 }; Vector* v1 = new Vector(s); Vector* v2 = v1->clone(); v1->show(); v2->show(); return 0; }
标签:vector,System,模式,Vector,原型,array,public,Tutorial,out From: https://www.cnblogs.com/manmmm/p/16810588.html