[JAVA] Java对象传递与返回的细节问题详析

2158 0
王子 2022-11-6 11:49:02 | 显示全部楼层 |阅读模式
目录

    1.传递引用2. 创建本地副本
      2.1 值传递2.2 对象克隆2.3 浅拷贝问题2.4 深拷贝
        2.4.1 引用类型逐个浅拷贝2.4.2 序列化方式进行深拷贝

    总结


1.传递引用

在一个方法中将一个对象的引用传递给另外一个方法,引用指向的对象是同一个
  1. public class Person {
  2.         int age;
  3.         String name;
  4.         public Person(int age, String name) {
  5.                 this.age = age;
  6.                 this.name = name;
  7.         }
  8.         public static void main(String[] args) {
  9.                 Person p=new Person(18, "tom");
  10.                 System.out.println("main:  "+p);
  11.                 f(p);
  12.         }
  13.         public static void f(Person p) {
  14.                 System.out.println("f():  "+p);
  15.         }
  16. }
复制代码


引用别名
  1. public static void main(String[] args) {
  2.                 Person p=new Person(18, "tom");
  3.                 Person p2=p;
  4.                 p2.age++;
  5.                 System.out.println(p.age);//19       
  6.         }
复制代码
引用p和p2指向的是同一个对象,p2对对象的属性进行操作,当使用引用p访问对象的属性时当然也改变,同样的情况也发生在对象引用在方法之间的传递,如下面的代码:
  1. public static void main(String[] args) {
  2.                 Person p=new Person(18, "tom");
  3.                 f(p);
  4.                 System.out.println(p.age);//19       
  5.         }
  6.         public static void f(Person p) {
  7.                 p.age++;
  8.         }
复制代码
2. 创建本地副本

几个概念:
    引用别名会在方法参数是对象类型时自动发生没有本地对象,只有本地引用(方法中创建的对象存在于堆中,只有引用变量存在于方法栈中)引用是有作用域的,而对象没有Java中的对象的生命周期并不是一个问题(垃圾回收机制)

2.1 值传递

Java中方法之间只有值传递
传递基本类型时,传递的是基本类型的值的拷贝
  1. public static void main(String[] args) {
  2.                 int a=100;
  3.                 change(a);
  4.                 System.out.println(a);//100 不受影响
  5.         }
  6.         public static void change(int a) {
  7.                 a=99;
  8.         }
复制代码
传递对象时,传递的是对象的引用拷贝
  1. public static void main(String[] args) {
  2.                 Person p=new Person(18, "tom");
  3.                 f(p);
  4.                 System.out.println(p.age);//还是18 f()中p指向了一个新的对象 不影响main函数
  5.         }
  6.         public static void f(Person p) {
  7.                 p=new Person(20, "bob");
  8.         }
复制代码
传递对象时如果在另外一个方法中对对象的属性进行操作会对main方法产生“副作用”, 但是如果只是简单的对引用进行操作是没有影响的

2.2 对象克隆

步骤:
类实现Cloneable空接口,默认情况下不希望所有的类都有克隆能力,当需要某个类有克隆能力时就需要实现该接口作为一种“可克隆”的标记,否则克隆时会报错CloneNotSupportedException
  1. public interface Cloneable {
  2. }
复制代码
重写clone方法,clone方法是Object类中的,它在Object类中的是一个protected的本地方法,需要重写,加上public修饰符,否则只能在当前类中使用clone方法
  1. protected native Object clone() throws CloneNotSupportedException;
复制代码
  1. public class Person implements Cloneable {
  2.         int age;
  3.         String name;
  4.         public Person(int age, String name) {
  5.                 this.age = age;
  6.                 this.name = name;
  7.         }
  8.         public Person clone() throws CloneNotSupportedException  {
  9.                         return (Person) super.clone();
  10.         }
  11.         public static void main(String[] args) throws CloneNotSupportedException {
  12.                 Person p1=new Person(18, "tom");
  13.                 Person p2=p1.clone();
  14.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age);
  15.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age);
  16.         }
  17. }
复制代码


重写clone方法时实际是就是调用Object类中的本地clone方法,Object类中的clone方法做了哪些工作?
Object类中clone方法负责创建正确大小的存储空间,并执行了从原始对象中所有二进制位到新对象内存中的按位复制。

2.3 浅拷贝问题

场景:Person类中增加一个引用类型的属性Country, 表示这个人所属的国家,然后进行克隆
  1. package test;
  2. class Country{
  3.         String nation;
  4.         public Country(String nation) {
  5.                 super();
  6.                 this.nation = nation;
  7.         }
  8. }
  9. public class Person implements Cloneable {
  10.         int age;
  11.         String name;
  12.         Country country;
  13.         public Person(int age, String name,Country country) {
  14.                 this.age = age;
  15.                 this.name = name;
  16.                 this.country=country;
  17.         }
  18.         public Person clone() throws CloneNotSupportedException  {
  19.                         return (Person) super.clone();
  20.         }
  21.         public static void main(String[] args) throws CloneNotSupportedException {
  22.                 Country country=new Country("China");
  23.                 Person p1=new Person(18, "tom",country);
  24.                 Person p2=p1.clone();
  25.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
  26.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
  27.                 p1.name="bob";
  28.                 p1.country.nation="America";
  29.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
  30.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
  31.         }
  32. }
复制代码


问题描述: 当Person类中有一个引用类型属性时,对于基本类型数据和String类型是对值进行拷贝的,因此改变p1的name不影响p2的name, 但是对于Country这种引用类型,在clone时仅仅是拷贝了一份对象的引用,拷贝的引用和原来的引用指向的是同一个对象,因此p1改变country的属性时,p2的country的属性也发生了改变

2.4 深拷贝

解决浅拷贝问题的关键点在于不仅仅是拷贝引用,而且要拷贝一份引用指向的对象,深拷贝有两种方式:
    逐个对引用指向的对象进行浅拷贝使用序列化方式进行深拷贝

2.4.1 引用类型逐个浅拷贝

如果一个类A中有多个引用类型,那么这些引用类型的类需要实现 Cloneable接口,在对A的对象进行克隆时,逐个浅拷贝其中的引用类型
eg: Person类中有Country引用类型,在进行clone时,单独对country进行浅拷贝
  1. package test;
  2. class Country implements Cloneable{
  3.         String nation;
  4.         public Country(String nation) {
  5.                 super();
  6.                 this.nation = nation;
  7.         }
  8.         public Country clone() throws CloneNotSupportedException  {
  9.                 return (Country) super.clone();
  10. }
  11. }
  12. public class Person implements Cloneable {
  13.         int age;
  14.         String name;
  15.         Country country;
  16.         public Person(int age, String name,Country country) {
  17.                 this.age = age;
  18.                 this.name = name;
  19.                 this.country=country;
  20.         }
  21.         public Person clone() throws CloneNotSupportedException  {
  22.                         Person p=null;
  23.                         p=(Person) super.clone();
  24.                         p.country=p.country.clone();//对country进行浅拷贝
  25.                         return p;
  26.         }
  27.         public static void main(String[] args) throws CloneNotSupportedException {
  28.                 Country country=new Country("China");
  29.                 Person p1=new Person(18, "tom",country);
  30.                 Person p2=p1.clone();
  31.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
  32.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
  33.                 p1.name="bob";
  34.                 p1.country.nation="America";
  35.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
  36.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
  37.         }
  38. }
复制代码



2.4.2 序列化方式进行深拷贝
  1. package test;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. class Country implements Serializable{
  9.         private static final long serialVersionUID = 1L;
  10.         String nation;
  11.         public Country(String nation) {
  12.                 super();
  13.                 this.nation = nation;
  14.         }
  15. }
  16. public class Person implements Cloneable,Serializable {
  17.         private static final long serialVersionUID = 1L;
  18.         int age;
  19.         String name;
  20.         Country country;
  21.         public Person(int age, String name,Country country) {
  22.                 this.age = age;
  23.                 this.name = name;
  24.                 this.country=country;
  25.         }
  26.         public Person clone() throws CloneNotSupportedException  {
  27.                         Person p=null;
  28.                         ObjectInputStream ois=null;
  29.                         ObjectOutputStream oos=null;
  30.                         ByteArrayInputStream bais=null;
  31.                         ByteArrayOutputStream baos=null;
  32.                         try {
  33.                                 baos=new ByteArrayOutputStream();
  34.                                 oos=new ObjectOutputStream(baos);
  35.                                 oos.writeObject(this);//Person对象序列化 序列化写入到baos流中
  36.                                 bais=new ByteArrayInputStream(baos.toByteArray());
  37.                                 ois=new ObjectInputStream(bais);//从baos流中读取数据到ois
  38.                                 p=(Person) ois.readObject();//ois读取对象数据
  39.                         } catch (Exception e) {
  40.                                 e.printStackTrace();
  41.                         }finally {
  42.                                 if(bais!=null)
  43.                                         try {
  44.                                                 bais.close();
  45.                                         } catch (IOException e) {
  46.                                                 e.printStackTrace();
  47.                                         }
  48.                                 if(baos!=null)
  49.                                         try {
  50.                                                 baos.close();
  51.                                         } catch (IOException e) {
  52.                                                 e.printStackTrace();
  53.                                         }
  54.                                 if(oos!=null)
  55.                                         try {
  56.                                                 oos.close();
  57.                                         } catch (IOException e) {
  58.                                                 e.printStackTrace();
  59.                                         }
  60.                                 if(ois!=null)
  61.                                         try {
  62.                                                 ois.close();
  63.                                         } catch (IOException e) {
  64.                                                 e.printStackTrace();
  65.                                         }
  66.                         }
  67.                         return p;
  68.         }
  69.         public static void main(String[] args) throws CloneNotSupportedException {
  70.                 Country country=new Country("China");
  71.                 Person p1=new Person(18, "tom",country);
  72.                 Person p2=p1.clone();
  73.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
  74.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
  75.                 p1.name="bob";
  76.                 p1.country.nation="America";
  77.                 System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
  78.                 System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
  79.         }
  80. }
复制代码


总结: 实现一个可克隆的类的步骤
    实现Cloneable接口重写clone方法在重写的clone方法中调用super.clone()方法在重写的clone方法中捕获异常

总结

到此这篇关于Java对象传递与返回细节问题的文章就介绍到这了,更多相关Java对象传递与返回内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行