當前位置:首頁 » 編程語言 » java的類的復制

java的類的復制

發布時間: 2022-11-27 15:58:37

java怎麼樣構造函數復制一個對象

一、用Object 本身的復制對象的方法, clone()。對象可克隆的類必須實現Cloneable介面,並且clone方法是淺克隆。
二、類實現Serializable,用ObjectOutputStream、ObjectInputStream 來復制對象。

對象克隆有點復雜,尤其是第一種。
關於這個問題你可以搜索:
JAVA深復制(深克隆)與淺復制(淺克隆)
這篇文章看看。

Ⅱ java 怎樣復制一個類

直接無視1樓
實現Cloneable介面,重載clone方法即可
public class A implements Cloneable {
public String name;

public Object clone() {
A o = null;
try {
o = (A) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}

}

Ⅲ java對象復制的問題

1.
被復制的類需要實現Clonenable介面(不實現的話在調用clone方法會拋出CloneNotSupportedException異常)
該介面為標記介面(不含任何方法)
2.
覆蓋clone()方法,訪問修飾符設為public。方法中調用super.clone()方法得到需要的復制對象,(native為本地方法)

Ⅳ java中如何將一個類的成員變數復制到另一個類中

用extends繼承,直接使用父類的變數,缺點:不能繼承其他類
用getter,通過getter獲取變數,缺點:過多setter和getter容易破壞代碼的可維護性
創建類的實例,然後調用實例的值,缺點:如果長時間不用實例化的對象,會被自動回收
適用reflection反射機制,導入java.lang.reflect包,以此來調用類裡面的變數,缺點:開發環境不可以有安全限制,可移植性差
用介面,把變數放到一個介面裡面,然後共享數據的類實現這個介面,缺點:暫時沒想到</ol>

Ⅳ Java中如何通過序列化進行深層復制

先創建兩個類,一個學生類和一個課程類(兩個類都要實現Serializable介面才能被序列化),學生類有一個屬性為課程。

importjava.io.Serializable;

/**
*課程類
*/
{
privateStringname;

publicCourse(Stringname){
this.name=name;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}
}
importjava.io.Serializable;

/**
*學生類
*/
{

//學號
privateintid;
//姓名
privateStringname;
//課程
privateCoursecourse;

publicStudent(){
}

publicStudent(intid,Stringname,Coursecourse){
this.id=id;
this.name=name;
this.course=course;
}

publicintgetId(){
returnid;
}

publicvoidsetId(intid){
this.id=id;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicCoursegetCourse(){
returncourse;
}

publicvoidsetCourse(Coursecourse){
this.course=course;
}
}

對象復制:

importjava.io.*;


publicclassObjectCopy{

publicstaticvoidmain(String[]args){

Coursecourse=newCourse("java程序設計");
Studentstu=newStudent(1,"jack",course);
System.out.println("原對象hashcode:"+stu.hashCode());
try{
//序列化
ByteArrayOutputStreambaos=newByteArrayOutputStream();
ObjectOutputStreamoos=newObjectOutputStream(baos);
oos.writeObject(stu);

//反序列化
ByteArrayInputStreams=newByteArrayInputStream(baos.toByteArray());
ObjectInputStreamois=newObjectInputStream(s);
//復制後的對象
Student=(Student)ois.readObject();
System.out.println("新對象hashcode:"+.hashCode());
System.out.println(.getCourse().getName());
}catch(IOExceptione){
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
}

Ⅵ Java 如何復制對象

可以使用clone來實現,clone用於為引用類型的復制
1.使用clone方法的類必須先實現Cloneable介面,不然clone方法會直接返回CloneNotSupportedException不支持克隆的異常
2、實現Cloneable介面的類應該使用公共方法重寫 Object.clone(它是受保護的)。某個對象實現了此介面就克隆它是不可能的。即使 clone 方法是反射性調用的,也無法保證它將獲得成功。
3、在Java.lang.Object類中克隆方法是這么定義的:
protected Object clone()
throws CloneNotSupportedException
創建並返回此對象的一個副本。表明是一個受保護的方法,同一個包中可見。

Ⅶ java中怎麼實現實體類的拷貝

Java實現文件拷貝其實質上就是使用java提供的三種文件流操作,位元組流,字元流,二進制流。
位元組流:FileInputStream 與 FileOutputStream
使用示例:
void File(File oldFile, File newFile){

FileOutputStream outputStream = new FileOutputStream(newFile,true);
FileInputStream inputStream = new FileInputStream(oldFile);
byte []wxj = new byte[1024];
int length = inputStream.read(wxj);
while(length!=-1){
outputStream.write(wxj,0,length);
length = inputStream.read(wxj);
}
}
字元流:FileReader 和 FileWriter
使用示例:
void File(File oldFile, File newFile){

Writer writer = new FileWriter(newFile,true);
Reader reader = new FileReader(oldFile);
char []wxj = new char[1024];
int length = reader.read(wxj);
while(length!=-1){
writer.write(wxj,0,length);
length = reader.read(wxj);
}
}
二進制流:DataInputStream 和 DataOutputStream
使用示例:
void File(File oldFile, File newFile){

FileOutputStream outputStream = new FileOutputStream(newFile,true);
FileInputStream inputStream = new FileInputStream(oldFile);
DataInputStream dataInput = new DataInputStream(inputStream);
DataOutputStream dataOutput = new DataOutputStream(outputStream);
byte []wxj = new byte[1024];
int length = dataInput.read(wxj);
while(length!=-1){
dataOutput.write(wxj,0,length);
length = dataInput.read(wxj);
}
}
總結一下:位元組流讀取文件的單位為位元組,對於英語字母(只佔一個位元組)不受任何影響,而對於中文文字在unicode編碼為兩個位元組(或者以上?)則可能會造成影響;字元流讀取文件的單位為字元,沒有上述位元組流的弊端,而且其提供緩沖區讀取/寫入,更加方便與高效;二進制流本質上也屬於位元組流,但是它在讀取/寫入文件時把文件內容轉化為二進制的方式讀取/寫入,不易出錯而且極為高效,一般用於讀取/寫入視頻等大文件信息。

Ⅷ java對象復制的問題

public class Exception

當調用 Object 類中的 clone 方法復制對象,但該對象的類無法實現 Cloneable 介面時,拋出該異常。

重寫 clone 方法的應用程序也可能拋出此異常,指示不能或不應復制一個對象。

以上引自java api 中文文檔

因為樓主的test沒有實現Cloneable介面..

Ⅸ java 編寫FileCopy類,要求將1個文件的內容同時復製成多個文件.使用命令行完成文件名的輸入。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.Scanner;

public class FileCopy {

public static void main(String[] args) throws Exception {

File f1 = new File("D:\\test\\test.txt");
String path = "D:\\test\\";
System.out.print("請輸入要復制的文件個數:");
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
for(int i = 0 ; i< cnt ; i++){
System.out.print("請輸入第"+(i+1)+"個文件名:");
String newName = sc.next();
System.out.println("第"+(i+1)+"個文件的名字為:"+newName+".txt");
File f2 = new File(path+newName+".txt");
forTransfer(f1,f2);
}

}
/**
* @author Samsung
* @date 2017年4月20日15:20:25
* 實現文件內容的復制
*
* */
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}

}

Ⅹ Java中,復制一個對象,有什麼好的方法

使用Java的反射機制實現:為了能更好的區分,寫成了兩個類,可以運行下面的代碼看看效果
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) throws Exception {
Customer1 c1 = new Customer1();
c1.setName("c1");
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
c1.setList(list);
Map<String,String> map = new HashMap<String, String>();
map.put("map1", "map1");
map.put("map2", "map2");
c1.setMap(map);
Customer2 c2 = new Customer2();
//
Class c = c1.getClass();
Class class2 = c2.getClass();
Field fields[] = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
Method getMethod = c.getMethod(getMethodName, new Class[] {});
Method setMethod = class2.getMethod(setMethodName,
new Class[] { field.getType() });
Object value = getMethod.invoke(c1, new Object[] {});
setMethod.invoke(c2, new Object[] { value });
}
System.out.println(c2.getName());
System.out.println(c2.getList());
System.out.println(c2.getMap());
}
}
class Customer1 {
private String name;
private List<String> list;
private Map<String, String> map;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
class Customer2 {
private String name;
private List<String> list;
private Map<String, String> map;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}

熱點內容
反編譯軟體id 發布:2024-04-20 10:29:49 瀏覽:44
視頻太長怎麼壓縮發微信 發布:2024-04-20 10:00:14 瀏覽:384
顯卡怎麼保存配置 發布:2024-04-20 09:28:52 瀏覽:596
校園交易網站源碼 發布:2024-04-20 09:18:54 瀏覽:701
江蘇北斗授時伺服器ip雲空間 發布:2024-04-20 08:53:50 瀏覽:931
dedecms批量上傳圖片 發布:2024-04-20 08:42:11 瀏覽:966
酷q如何編譯 發布:2024-04-20 08:41:27 瀏覽:79
安卓手機數字人民幣怎麼下載 發布:2024-04-20 08:38:21 瀏覽:114
access如何配置資料庫 發布:2024-04-20 08:37:35 瀏覽:504
手寫輸入演算法 發布:2024-04-20 08:29:31 瀏覽:258