当前位置:首页 » 编程语言 » 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;
}
}

热点内容
二级程序编译答案 发布:2024-05-03 18:41:35 浏览:652
领动自动精英版是哪个配置 发布:2024-05-03 18:37:30 浏览:149
java编译器中cd什么意思 发布:2024-05-03 18:36:00 浏览:388
传奇服务器如何刷钱 发布:2024-05-03 18:36:00 浏览:976
安卓版twitter怎么注册 发布:2024-05-03 18:28:05 浏览:893
Python逻辑优先级 发布:2024-05-03 18:26:14 浏览:266
linux查看svn密码 发布:2024-05-03 18:12:47 浏览:803
地铁逃生怎么进入游戏安卓 发布:2024-05-03 17:49:35 浏览:992
aws云存储 发布:2024-05-03 17:48:50 浏览:954
安卓微信王者号怎么转成苹果 发布:2024-05-03 17:44:38 浏览:745