欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java對(duì)象傳遞與返回的細(xì)節(jié)問(wèn)題詳析

 更新時(shí)間:2022年11月04日 11:15:09   作者:CodePanda@GPF  
我們知道這是一個(gè)核心概念,在Java中總是按值傳遞而不是按引用傳遞,下面這篇文章主要給大家介紹了關(guān)于Java對(duì)象傳遞與返回的細(xì)節(jié)問(wèn)題的相關(guān)資料,需要的朋友可以參考下

1.傳遞引用

在一個(gè)方法中將一個(gè)對(duì)象的引用傳遞給另外一個(gè)方法,引用指向的對(duì)象是同一個(gè)

public class Person {
	int age;
	String name;
	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}
	
	public static void main(String[] args) {
		Person p=new Person(18, "tom");
		System.out.println("main:  "+p);
		f(p);
	}
	public static void f(Person p) {
		System.out.println("f():  "+p);
	}
}

引用別名

public static void main(String[] args) {
		Person p=new Person(18, "tom");
		Person p2=p;
		p2.age++;
		System.out.println(p.age);//19	
	}
	

引用p和p2指向的是同一個(gè)對(duì)象,p2對(duì)對(duì)象的屬性進(jìn)行操作,當(dāng)使用引用p訪問(wèn)對(duì)象的屬性時(shí)當(dāng)然也改變,同樣的情況也發(fā)生在對(duì)象引用在方法之間的傳遞,如下面的代碼:

public static void main(String[] args) {
		Person p=new Person(18, "tom");
		f(p);
		System.out.println(p.age);//19	
	}
	public static void f(Person p) {
		p.age++;
	}

2. 創(chuàng)建本地副本

幾個(gè)概念:

  • 引用別名會(huì)在方法參數(shù)是對(duì)象類(lèi)型時(shí)自動(dòng)發(fā)生
  • 沒(méi)有本地對(duì)象,只有本地引用(方法中創(chuàng)建的對(duì)象存在于堆中,只有引用變量存在于方法棧中)
  • 引用是有作用域的,而對(duì)象沒(méi)有
  • Java中的對(duì)象的生命周期并不是一個(gè)問(wèn)題(垃圾回收機(jī)制)

2.1 值傳遞

Java中方法之間只有值傳遞

傳遞基本類(lèi)型時(shí),傳遞的是基本類(lèi)型的值的拷貝

public static void main(String[] args) {
		int a=100;
		change(a);
		System.out.println(a);//100 不受影響
	}
	public static void change(int a) {
		a=99;
	}

傳遞對(duì)象時(shí),傳遞的是對(duì)象的引用拷貝

public static void main(String[] args) {
		Person p=new Person(18, "tom");
		f(p);
		System.out.println(p.age);//還是18 f()中p指向了一個(gè)新的對(duì)象 不影響main函數(shù)
	}
	public static void f(Person p) {
		p=new Person(20, "bob");
	}

傳遞對(duì)象時(shí)如果在另外一個(gè)方法中對(duì)對(duì)象的屬性進(jìn)行操作會(huì)對(duì)main方法產(chǎn)生“副作用”, 但是如果只是簡(jiǎn)單的對(duì)引用進(jìn)行操作是沒(méi)有影響的

2.2 對(duì)象克隆

步驟:

類(lèi)實(shí)現(xiàn)Cloneable空接口,默認(rèn)情況下不希望所有的類(lèi)都有克隆能力,當(dāng)需要某個(gè)類(lèi)有克隆能力時(shí)就需要實(shí)現(xiàn)該接口作為一種“可克隆”的標(biāo)記,否則克隆時(shí)會(huì)報(bào)錯(cuò)CloneNotSupportedException

public interface Cloneable {
}

重寫(xiě)clone方法,clone方法是Object類(lèi)中的,它在Object類(lèi)中的是一個(gè)protected的本地方法,需要重寫(xiě),加上public修飾符,否則只能在當(dāng)前類(lèi)中使用clone方法

 protected native Object clone() throws CloneNotSupportedException;
public class Person implements Cloneable {
	int age;
	String name;
	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}
	
	public Person clone() throws CloneNotSupportedException  {
			return (Person) super.clone();
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Person p1=new Person(18, "tom");
		Person p2=p1.clone();
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age);
	}
}

重寫(xiě)clone方法時(shí)實(shí)際是就是調(diào)用Object類(lèi)中的本地clone方法,Object類(lèi)中的clone方法做了哪些工作?

Object類(lèi)中clone方法負(fù)責(zé)創(chuàng)建正確大小的存儲(chǔ)空間,并執(zhí)行了從原始對(duì)象中所有二進(jìn)制位到新對(duì)象內(nèi)存中的按位復(fù)制。

2.3 淺拷貝問(wèn)題

場(chǎng)景:Person類(lèi)中增加一個(gè)引用類(lèi)型的屬性Country, 表示這個(gè)人所屬的國(guó)家,然后進(jìn)行克隆

package test;

class Country{
	String nation;

	public Country(String nation) {
		super();
		this.nation = nation;
	}
	
}
public class Person implements Cloneable {
	int age;
	String name;
	Country country;
	public Person(int age, String name,Country country) {
		this.age = age;
		this.name = name;
		this.country=country;
	}
	
	public Person clone() throws CloneNotSupportedException  {
			return (Person) super.clone();
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Country country=new Country("China");
		Person p1=new Person(18, "tom",country);
		Person p2=p1.clone();
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
		p1.name="bob";
		p1.country.nation="America";
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
	}
}

問(wèn)題描述: 當(dāng)Person類(lèi)中有一個(gè)引用類(lèi)型屬性時(shí),對(duì)于基本類(lèi)型數(shù)據(jù)和String類(lèi)型是對(duì)值進(jìn)行拷貝的,因此改變p1的name不影響p2的name, 但是對(duì)于Country這種引用類(lèi)型,在clone時(shí)僅僅是拷貝了一份對(duì)象的引用,拷貝的引用和原來(lái)的引用指向的是同一個(gè)對(duì)象,因此p1改變country的屬性時(shí),p2的country的屬性也發(fā)生了改變

2.4 深拷貝

解決淺拷貝問(wèn)題的關(guān)鍵點(diǎn)在于不僅僅是拷貝引用,而且要拷貝一份引用指向的對(duì)象,深拷貝有兩種方式:

  • 逐個(gè)對(duì)引用指向的對(duì)象進(jìn)行淺拷貝
  • 使用序列化方式進(jìn)行深拷貝

2.4.1 引用類(lèi)型逐個(gè)淺拷貝

如果一個(gè)類(lèi)A中有多個(gè)引用類(lèi)型,那么這些引用類(lèi)型的類(lèi)需要實(shí)現(xiàn) Cloneable接口,在對(duì)A的對(duì)象進(jìn)行克隆時(shí),逐個(gè)淺拷貝其中的引用類(lèi)型

eg: Person類(lèi)中有Country引用類(lèi)型,在進(jìn)行clone時(shí),單獨(dú)對(duì)country進(jìn)行淺拷貝

package test;

class Country implements Cloneable{
	String nation;

	public Country(String nation) {
		super();
		this.nation = nation;
	}
	public Country clone() throws CloneNotSupportedException  {
		return (Country) super.clone();
}
	
}
public class Person implements Cloneable {
	int age;
	String name;
	Country country;
	public Person(int age, String name,Country country) {
		this.age = age;
		this.name = name;
		this.country=country;
	}
	
	public Person clone() throws CloneNotSupportedException  {
			
			Person p=null;
			p=(Person) super.clone();
			p.country=p.country.clone();//對(duì)country進(jìn)行淺拷貝
			return p;
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Country country=new Country("China");
		Person p1=new Person(18, "tom",country);
		Person p2=p1.clone();
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
		p1.name="bob";
		p1.country.nation="America";
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
		
	}
}

2.4.2 序列化方式進(jìn)行深拷貝

package test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Country implements Serializable{

	private static final long serialVersionUID = 1L;
	String nation;

	public Country(String nation) {
		super();
		this.nation = nation;
	}
	
}
	

public class Person implements Cloneable,Serializable {
	private static final long serialVersionUID = 1L;
	int age;
	String name;
	Country country;
	public Person(int age, String name,Country country) {
		this.age = age;
		this.name = name;
		this.country=country;
	}
	
	public Person clone() throws CloneNotSupportedException  {
			
			Person p=null;
			ObjectInputStream ois=null;
			ObjectOutputStream oos=null;
			ByteArrayInputStream bais=null;
			ByteArrayOutputStream baos=null;
			
			try {
				baos=new ByteArrayOutputStream();
				oos=new ObjectOutputStream(baos);
				oos.writeObject(this);//Person對(duì)象序列化 序列化寫(xiě)入到baos流中
				
				
				bais=new ByteArrayInputStream(baos.toByteArray());
				ois=new ObjectInputStream(bais);//從baos流中讀取數(shù)據(jù)到ois
				p=(Person) ois.readObject();//ois讀取對(duì)象數(shù)據(jù)
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				if(bais!=null)
					try {
						bais.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				if(baos!=null)
					try {
						baos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				if(oos!=null)
					try {
						oos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				if(ois!=null)
					try {
						ois.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}

			return p;
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Country country=new Country("China");
		Person p1=new Person(18, "tom",country);
		Person p2=p1.clone();
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country);
		p1.name="bob";
		p1.country.nation="America";
		System.out.println(p1+"   name: "+p1.name+"  age: "+p1.age+"  country: "+p1.country.nation);
		System.out.println(p2+"   name: "+p2.name+"  age: "+p2.age+"  country: "+p2.country.nation);
		
	}
}

總結(jié): 實(shí)現(xiàn)一個(gè)可克隆的類(lèi)的步驟

  • 實(shí)現(xiàn)Cloneable接口
  • 重寫(xiě)clone方法
  • 在重寫(xiě)的clone方法中調(diào)用super.clone()方法
  • 在重寫(xiě)的clone方法中捕獲異常

總結(jié)

到此這篇關(guān)于Java對(duì)象傳遞與返回細(xì)節(jié)問(wèn)題的文章就介紹到這了,更多相關(guān)Java對(duì)象傳遞與返回內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java寫(xiě)入寫(xiě)出Excel操作源碼分享

    Java寫(xiě)入寫(xiě)出Excel操作源碼分享

    這篇文章主要介紹了Java寫(xiě)入寫(xiě)出Excel操作源碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 如何把JAR發(fā)布到maven中央倉(cāng)庫(kù)的幾種方法

    如何把JAR發(fā)布到maven中央倉(cāng)庫(kù)的幾種方法

    這篇文章主要介紹了如何把JAR發(fā)布到maven中央倉(cāng)庫(kù)的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 淺談java多線程編程

    淺談java多線程編程

    這篇文章主要介紹了java多線程編程的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)java多線程,感興趣的朋友可以了解下
    2020-08-08
  • 詳解用java描述矩陣求逆的算法

    詳解用java描述矩陣求逆的算法

    這篇文章主要介紹了用java描述矩陣求逆的算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例

    java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例

    這篇文章主要給大家介紹了關(guān)于java字符串轉(zhuǎn)JSON的相關(guān)資料,JSON?是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于Web應(yīng)用程序中的數(shù)據(jù)傳輸,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Java多線程死鎖問(wèn)題詳解(wait和notify)

    Java多線程死鎖問(wèn)題詳解(wait和notify)

    線程之間形成相互等待資源的環(huán)時(shí),就會(huì)形成順序死鎖,下面這篇文章主要給大家介紹了關(guān)于Java多線程死鎖問(wèn)題(wait和notify)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題

    java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題

    這篇文章主要介紹了java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼

    Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 使用Java代碼將IP地址轉(zhuǎn)換為int類(lèi)型的方法

    使用Java代碼將IP地址轉(zhuǎn)換為int類(lèi)型的方法

    這篇文章主要介紹了使用Java代碼將IP地址轉(zhuǎn)換為int類(lèi)型的方法,這也是各大計(jì)算機(jī)考試和ACM以及面試的常見(jiàn)基礎(chǔ)問(wèn)題,需要的朋友可以參考下
    2015-08-08
  • idea 右鍵項(xiàng)目沒(méi)有run 運(yùn)行選項(xiàng)

    idea 右鍵項(xiàng)目沒(méi)有run 運(yùn)行選項(xiàng)

    這篇文章主要介紹了idea 右鍵項(xiàng)目沒(méi)有run 運(yùn)行選項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論