首页
/
每日頭條
/
職場
/
面試需要問面試官些什麼問題
面試需要問面試官些什麼問題
更新时间:2026-03-29 06:26:59
一、拷貝的引入(1)、引用拷貝

創建一個指向對象的引用變量的拷貝。

Teacherteacher=newTeacher("Taylor",26); Teacherotherteacher=teacher; System.out.println(teacher); System.out.println(otherteacher);

輸出結果:

blog.Teacher@355da254 blog.Teacher@355da254

結果分析:由輸出結果可以看出,它們的地址值是相同的,那麼它們肯定是同一個對象。teacher和otherteacher的隻是引用而已,他們都指向了一個相同的對象Teacher(“Taylor”,26)。這就叫做引用拷貝。往期:一百期面試題彙總

面試需要問面試官些什麼問題(面試官問點兒基礎的)1

(2)、對象拷貝

創建對象本身的一個副本。

Teacherteacher=newTeacher("Swift",26); Teacherotherteacher=(Teacher)teacher.clone(); System.out.println(teacher); System.out.println(otherteacher);

輸出結果:

blog.Teacher@355da254 blog.Teacher@4dc63996

結果分析:由輸出結果可以看出,它們的地址是不同的,也就是說創建了新的對象, 而不是把原對象的地址賦給了一個新的引用變量,這就叫做對象拷貝。

面試需要問面試官些什麼問題(面試官問點兒基礎的)2

注:深拷貝和淺拷貝都是對象拷貝

二、淺拷貝(1)、定義

被複制對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用仍然指向原來的對象。即對象的淺拷貝會對“主”對象進行拷貝,但不會複制主對象裡面的對象。”裡面的對象“會在原來的對象和它的副本之間共享。往期:100期面試題彙總

簡而言之,淺拷貝僅僅複制所考慮的對象,而不複制它所引用的對象

(2)、淺拷貝實例

packagecom.test; publicclassShallowCopy{ publicstaticvoidmain(String[]args)throwsCloneNotSupportedException{ Teacherteacher=newTeacher(); teacher.setName("riemann"); teacher.setAge(27); Student2student1=newStudent2(); student1.setName("edgar"); student1.setAge(18); student1.setTeacher(teacher); Student2student2=(Student2)student1.clone(); System.out.println("拷貝後"); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); System.out.println("修改老師的信息後-------------"); //修改老師的信息 teacher.setName("Games"); System.out.println(student1.getTeacher().getName()); System.out.println(student2.getTeacher().getName()); } } classTeacherimplementsCloneable{ privateStringname; privateintage; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } } classStudent2implementsCloneable{ privateStringname; privateintage; privateTeacherteacher; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicTeachergetTeacher(){ returnteacher; } publicvoidsetTeacher(Teacherteacher){ this.teacher=teacher; } publicobjectclone()throwsCloneNotSupportedException{ Objectobject=super.clone(); returnobject; } }

輸出結果:

拷貝後 edgar 18 riemann 27 修改老師的信息後------------- Games Games

結果分析:兩個引用student1和student2指向不同的兩個對象,但是兩個引用student1和student2中的兩個teacher引用指向的是同一個對象,所以說明是淺拷貝。

面試需要問面試官些什麼問題(面試官問點兒基礎的)3

三、深拷貝(1)、定義

深拷貝是一個整個獨立的對象拷貝,深拷貝會拷貝所有的屬性,并拷貝屬性指向的動态分配的内存。當對象和它所引用的對象一起拷貝時即發生深拷貝。深拷貝相比于淺拷貝速度較慢并且花銷較大。

簡而言之,深拷貝把要複制的對象所引用的對象都複制了一遍。

往期:100期面試題彙總

(2)、深拷貝實例

packagecom.test; publicclassDeepCopy{ publicstaticvoidmain(String[]args)throwsCloneNotSupportedException{ Teacher2teacher=newTeacher2(); teacher.setName("riemann"); teacher.setAge(27); Student3student1=newStudent3(); student1.setName("edgar"); student1.setAge(18); student1.setTeacher(teacher); student3student2=(Student3)student1.clone(); System.out.println("拷貝後"); System.out.println(student2.getName()); System.out.println(student2.getAge()); System.out.println(student2.getTeacher().getName()); System.out.println(student2.getTeacher().getAge()); System.out.println("修改老師的信息後-------------"); //修改老師的信息 teacher.setName("Games"); System.out.println(student1.getTeacher().getName()); System.out.println(student2.getTeacher().getName()); } } classTeacher2implementsCloneable{ privateStringname; privateintage; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicObjectclone()throwsCloneNotSupportedException{ returnsuper.clone(); } } classStudent3implementsCloneable{ privateStringname; privateintage; privateTeacher2teacher; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicTeacher2getTeacher(){ returnteacher; } publicvoidsetTeacher(Teacher2teacher){ this.teacher=teacher; } publicObjectclone()throwsCloneNotSupportedException{ //淺複制時: //Objectobject=super.clone(); //returnobject; //改為深複制: Student3student=(Student3)super.clone(); //本來是淺複制,現在将Teacher對象複制一份并重新set進來 student.setTeacher((Teacher2)student.getTeacher().clone()); returnstudent; } }

輸出結果:

拷貝後 edgar 18 riemann 27 修改老師的信息後------------- Games riemann

結果分析:

兩個引用student1和student2指向不同的兩個對象,兩個引用student1和student2中的兩個teacher引用指向的是兩個對象,但對teacher對象的修改隻能影響student1對象,所以說是深拷貝。

面試需要問面試官些什麼問題(面試官問點兒基礎的)4

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
dnf黑暗武士技能設置加點
dnf黑暗武士技能設置加點
DNF黑暗武士是一個比較好玩的職業,因為這個職業有别的四個鬼劍士的大部分技能,而且刷圖比較快,但是很多人都不會加點和組合技能!下面給大家介紹一下新版黑暗武士的技能加點及搭配方案:剩餘的SP點可以加再自己喜歡的技能上,現在版本的黑暗武士技能更...
2026-03-29
馬伊琍和朱亞文離婚片段
馬伊琍和朱亞文離婚片段
我曾經以為《蝸居》裡海藻的戲份是國産劇的最大尺度。顯然《北上廣不相信眼淚》突破了這個尺度。乍一看劇名,立刻就想起了每逢過年都要唇槍舌戰一番的「在北上廣生活還是回老家生活」辯題……感覺到一大波北上廣擠着地鐵心懷自由夢買不起房的小青年即将熱淚盈...
2026-03-29
如何快速成為森林騎士
如何快速成為森林騎士
謹以此文/獻給正在前行路上糾結如何選擇的你青年的小黑馬,它渴望大森林裡的浩瀚林海,一天它終于下決心決定進森林裡去轉一轉,在森林裡走着走着突然它迷失了方向,在前方出現了好幾條可以選擇走的路,茫茫的樹林、看不清前方到底會出現什麼。不知道眼前該走...
2026-03-29
内向的人比較适合什麼工作
内向的人比較适合什麼工作
内向的人,沒必要強行外向,選擇這幾項工作更容易成功前言:我們都知道人的個性分為外向和内向,大家都感覺外向的人在這個社會上非常的吃香很容易找到工作,内向的人很多工作都不适合他,而且工作起來很吃力。内向和外向其實從來都沒有優劣之分,隻是兩種性格...
2026-03-29
于謙和老婆白慧明
于謙和老婆白慧明
要說這幾年最有知名度的相聲演員是誰?于謙,肯定得算一個。他和郭德綱的相聲,可以說撐起了德雲社的半邊天。兩個人因為常在線下的場子裡表演,所以經常說一些“家長裡短”。于謙的“七大姑八大姨”,幾乎都成了郭德綱的創作素材。而于謙的老婆白慧明、郭德綱...
2026-03-29
Copyright 2023-2026 - www.tftnews.com All Rights Reserved