首页
/
每日頭條
/
職場
/
面試需要問面試官些什麼問題
面試需要問面試官些什麼問題
更新时间:2025-11-27 09:16:46
一、拷貝的引入(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
推荐阅读
王中磊跟誰結婚(曾為明星老闆王中磊做婚禮的他)
王中磊跟誰結婚(曾為明星老闆王中磊做婚禮的他)
     天色垂暮,微光漸起,在三亞瑞吉酒店最美的無邊泳池,一場盛大的婚禮要來了!600平米無邊泳池抽、蓄水将近2噸,徒手改造變成儀式區;4米高假山上直接覆蓋搭橋,壘台階搭扶手變成新娘出場區;容納400位賓客的1200平米棚房,從無到有拔地而起變為宴會區。200名工人,打通酒店中軸線,八天十晚嘔心醞釀呈現。以天為幕、以海為景,傍晚的彩霞為新娘披上了華美的嫁衣...
2025-11-27
青少年勞動技能教育(勞動光榮技能寶貴)
青少年勞動技能教育(勞動光榮技能寶貴)
     職業教育是國民教育體系和人力資源開發的重要組成部分,是廣大青年打開通往成功成才大門的重要途徑,我市廣大教育工作者努力工作,潛心創造,使我市職業教育取得了長足發展。   近年來,我市培養、培訓了各級各類實用人才6萬多人,涉及财經管理、機械電子、建築、涉農、藝術、幼教、醫護、餐飲、其他等9大門類94個專業。僅2016年,在省級比賽中我市就獲得四個一等獎...
2025-11-27
蘇州市自由職業者需要什麼資料(對應的男女職業什麼時候補全)
蘇州市自由職業者需要什麼資料(對應的男女職業什麼時候補全)
  現在DNF的職業體系中,不少都是男女對應職業。   比如男/女鬼劍、男/女槍手、男/女格鬥、男/女魔法師、男/女聖職者。   這五個古老的職業系,現今都有了男/女角色分支。   在對應的男/女職業出現之前,職業性轉是二創作者們創作的高頻地帶。   譬如還未出現的女聖職第五職業,多數人将其職業定義在了女藍拳上,由此誕生出了相應的性轉作品。         ...
2025-11-27
慰問勞動模範困難群衆(區領導開展新春慰問活動)
慰問勞動模範困難群衆(區領導開展新春慰問活動)
  新春佳節到來之際,帶着對困難黨員、勞動模範、一線職工的溫暖關愛,區委書記李學義,區委副書記、區長胡學明,區人大常委會主任楊文璁,區政協主席王潤生,區委副書記、區委組織部部長梁春早等區領導,深入基層單位、街道社區走訪慰問,送去黨和政府的深切關懷,向他們緻以新春的美好祝福。   李學義深入友誼路街誼城公寓社區慰問困難黨員,關切地詢問困難黨員的家庭生活、身體狀...
2025-11-27
如何與disc4種性格的人員進行溝通(掌握DISC巧妙影響領導)
如何與disc4種性格的人員進行溝通(掌握DISC巧妙影響領導)
     工作中,你有沒有遇到過這樣的情況:   開會時,領導提出了一個方案,可是你有更好的方案。當你提出時,卻被領導當衆否定。   此時你會怎麼想?   有人可能會想:“領導太武斷了,我該怎麼改變他?”   還有人則可能會想:“好了,那以後你說A,我也不說B了,以後哪怕有想法,我也不會講出來了。”      第一種想法很難實現,因為想要改變一個人,尤其對方...
2025-11-27
Copyright 2023-2025 - www.tftnews.com All Rights Reserved