最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.傳遞引用

在一個(gè)方法中將一個(gè)對象的引用傳遞給另外一個(gè)方法,引用指向的對象是同一個(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è)對象,p2對對象的屬性進(jìn)行操作,當(dāng)使用引用p訪問對象的屬性時(shí)當(dāng)然也改變,同樣的情況也發(fā)生在對象引用在方法之間的傳遞,如下面的代碼:

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ù)是對象類型時(shí)自動(dòng)發(fā)生
  • 沒有本地對象,只有本地引用(方法中創(chuàng)建的對象存在于堆中,只有引用變量存在于方法棧中)
  • 引用是有作用域的,而對象沒有
  • Java中的對象的生命周期并不是一個(gè)問題(垃圾回收機(jī)制)

2.1 值傳遞

Java中方法之間只有值傳遞

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

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

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

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

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

2.2 對象克隆

步驟:

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

public interface Cloneable {
}

重寫clone方法,clone方法是Object類中的,它在Object類中的是一個(gè)protected的本地方法,需要重寫,加上public修飾符,否則只能在當(dāng)前類中使用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);
	}
}

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

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

2.3 淺拷貝問題

場景:Person類中增加一個(gè)引用類型的屬性Country, 表示這個(gè)人所屬的國家,然后進(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);
	}
}

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

2.4 深拷貝

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

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

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

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

eg: Person類中有Country引用類型,在進(jìn)行clone時(shí),單獨(dú)對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();//對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對象序列化 序列化寫入到baos流中
				
				
				bais=new ByteArrayInputStream(baos.toByteArray());
				ois=new ObjectInputStream(bais);//從baos流中讀取數(shù)據(jù)到ois
				p=(Person) ois.readObject();//ois讀取對象數(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è)可克隆的類的步驟

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

總結(jié)

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

相關(guān)文章

  • 利用Java+OpenCV實(shí)現(xiàn)拍照功能

    利用Java+OpenCV實(shí)現(xiàn)拍照功能

    網(wǎng)上大多是利用C語言或者Python實(shí)現(xiàn)拍照功能,本文將為大家介紹另一種方法,即在Java中調(diào)用OpenCV實(shí)現(xiàn)拍照功能,感興趣的可以了解一下
    2022-01-01
  • WeakHashMap的垃圾回收原理詳解

    WeakHashMap的垃圾回收原理詳解

    這篇文章主要介紹了WeakHashMap的垃圾回收原理詳解,WeakHashMap 與 HashMap 的用法基本類似,與 HashMap 的區(qū)別在于,HashMap的key保留了對實(shí)際對象的強(qiáng)引用個(gè),這意味著只要該HashMap對象不被銷毀,該HashMap的所有key所引用的對象就不會(huì)被垃圾回收,需要的朋友可以參考下
    2023-09-09
  • Intellij IDEA連接Navicat數(shù)據(jù)庫的方法

    Intellij IDEA連接Navicat數(shù)據(jù)庫的方法

    這篇文章主要介紹了Intellij IDEA連接Navicat數(shù)據(jù)庫的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借價(jià)值,需要的朋友可以參考下
    2021-03-03
  • IDEA如何一鍵部署SpringBoot項(xiàng)目到服務(wù)器

    IDEA如何一鍵部署SpringBoot項(xiàng)目到服務(wù)器

    文章介紹了如何在IDEA中部署SpringBoot項(xiàng)目到服務(wù)器,使用AlibabaCloudToolkit插件進(jìn)行配置部署,步驟包括設(shè)置服務(wù)名稱、選擇文件上傳類型、選擇jar文件、添加服務(wù)器信息、輸入上傳路徑、選擇上傳后執(zhí)行的腳本以及執(zhí)行前的操作命令
    2024-12-12
  • IDEA設(shè)置生成帶注釋的getter和setter的圖文教程

    IDEA設(shè)置生成帶注釋的getter和setter的圖文教程

    通常我們用idea默認(rèn)生成的getter和setter方法是不帶注釋的,當(dāng)然,我們同樣可以設(shè)置idea像MyEclipse一樣生成帶有Javadoc的模板,具體設(shè)置方法,大家參考下本文
    2018-05-05
  • 詳解Java目錄操作與文件操作教程

    詳解Java目錄操作與文件操作教程

    本章具體介紹了目錄操作、文件操作的基本使用方法和常用函數(shù),圖解穿插代碼實(shí)現(xiàn),感興趣的朋友來看看吧
    2022-03-03
  • Java訪問修飾符public、private、protected及默認(rèn)訪問權(quán)限詳解

    Java訪問修飾符public、private、protected及默認(rèn)訪問權(quán)限詳解

    這篇文章主要介紹了Java訪問修飾符public、private、protected及默認(rèn)訪問權(quán)限的相關(guān)資料,每種修飾符都有其特定的使用場景,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-01-01
  • SpringBoot集成ActiveMQ的實(shí)戰(zhàn)全過程

    SpringBoot集成ActiveMQ的實(shí)戰(zhàn)全過程

    消息隊(duì)列中間件是分布式系統(tǒng)中重要的組件,主要解決應(yīng)用耦合、異步消息、流量削鋒等問題,實(shí)現(xiàn)高性能、高可用、可伸縮和最終一致性架構(gòu),是大型分布式系統(tǒng)不可缺少的中間件,這篇文章主要給大家介紹了關(guān)于SpringBoot集成ActiveMQ的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 最新評論

    荆州市| 夏河县| 长治市| 黔江区| 潼关县| 特克斯县| 阳山县| 双峰县| 高密市| 桂东县| 天台县| 伊春市| 金华市| 城固县| 白沙| 富川| 延川县| 全椒县| 吐鲁番市| 江油市| 海安县| 宽甸| 肥城市| 博乐市| 济阳县| 屏东市| 开封县| 大竹县| 江华| 昌图县| 屯昌县| 昌吉市| 忻城县| 玛曲县| 甘孜| 双江| 克什克腾旗| 海安县| 漠河县| 辽宁省| 唐海县|