首页
/
每日頭條
/
職場
/
面試需要問面試官些什麼問題
面試需要問面試官些什麼問題
更新时间:2026-10-22 13:39:07
一、拷貝的引入(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
推荐阅读
試睡師需要什麼工作
試睡師需要什麼工作
如果有人告訴你,睡覺就能賺錢,你想不想試試呢?今天小編就來帶你了解一個月入不到十萬的職業-試睡師!能參與這樣測試的酒店一般是對自己要求比較高的星級酒店或者高端名宿,這樣的酒店各方面的配套施設都是非常齊全的,那我們試睡師的重點工作是在哪裡呢?...
2026-10-22
活性炭廢氣吸附之後怎麼處理
活性炭廢氣吸附之後怎麼處理
企業老闆還在為處理有機廢氣煩惱嗎?您應該了解一下活性炭吸附法工業有機廢氣是指工業生産過程中排出的含揮發性有機物的氣态污染物,這些廢氣如果得不到有效處理會對工作人員和周圍環境造成嚴重的危害。因此治理有機廢氣,讓廢氣排放達到環保标準成了很多生産...
2026-10-22
被同事落井下石不信任怎麼辦
被同事落井下石不信任怎麼辦
剛剛和一個之前項目的同事通電話,挂完電話,我心裡久久不能平靜…他和我是老鄉,年紀比我父親小幾歲,論輩分,我得叫他一聲叔。他是個木工班組的組長,我們之前一直有合作,他做事情還比較認真,隻是為人比較小氣,話多而已!自從上一個項目結束,我們聯系也...
2026-10-22
勞動仲裁可以把律師費算上嗎
勞動仲裁可以把律師費算上嗎
01兩起判決先看兩起勞動仲裁的法院判決結果。案例1案号:(2021)粵03民終18040号一審判決:……二、**公司應于本判決生效之日起三日内支付董某律師費3772元;……雙方均不服一審判決,上訴至二審法院。二審判決:……二、變更廣東省深圳...
2026-10-22
肌底精華使用順序
肌底精華使用順序
随着國慶離我們逐漸遠去,這意味着2018年的所有假期都結束啦!醒一醒,打起精神工作喽!這假期回來的第一篇實驗測評,我們将鏡頭對準了這些【肌底精華】比起這兩年流行的“一夜回春安瓶”“貴婦級面霜”它們可就低調多了~勤勤懇懇地在肌底工作,說是要從...
2026-10-22
Copyright 2023-2026 - www.tftnews.com All Rights Reserved