Code about Java Clone (shallow clone)

Source: Internet
Author: User

1 Importjava.util.Date;2 3  Public classhelloworld{4      Public Static voidMain (string[] args)throwsclonenotsupportedexception{5Student STU1 =NewStudent ();6Student STU2 =(Student) Stu1.clone (STU1);7         8 System.out.println (STU1);9 System.out.println (STU2);TenSystem.out.println (STU1 = =stu2); OneSystem.out.println (Stu1.createdate = =stu2.createdate); A          -Stu2.createdate =NewDate (); -Stu2.name = "TomCat"; theStu2.age = 2333; -          - System.out.println (STU1); - System.out.println (STU2); +          -System.out.println (Stu1.createdate = =stu2.createdate); +          A     } at } - classStudentImplementscloneable { -      PublicDate CreateDate =NewDate (); -      PublicString name; -      Public intAge ; -      in      PublicStudent () { -          This("Stupid", 18); to     } +      -      PublicStudent (String name,intAge ) { the          This. Name =name; *          This. Age =Age ; $     }Panax Notoginseng      -      PublicObject Clone (Object o)throwsclonenotsupportedexception { the         return Super. Clone (); +     } A  the @Override +      PublicString toString () { -         return"Student [createdate=" + CreateDate + ", name=" + name + ", age=" + Age + "]"; $     } $}
Student [Createdate=fri Jul -  +: $: .Cst ., Name=stupid, age= -]student [CreateDate=fri Jul -  +: $: .Cst ., Name=stupid, age= -]falsetrueStudent [CreateDate=fri Jul -  +: $: .Cst ., Name=stupid, age= -]student [CreateDate=fri Jul -  +: $: .Cst ., Name=tomcat, age=2333]false

New Date (); Is there an object that has been created whose reference is Stu2.createdate

Qaq I was wrong, I was wrong, I was wrong, I was wrong

Should be written in the following way

1 stu2.createDate.setYear (1900);
Student [Createdate=fri Jul -  +:Wuyi: toCst ., Name=stupid, age= -]student [CreateDate=fri Jul -  +:Wuyi: toCst ., Name=stupid, age= -]falsetrueStudent [CreateDate=mon Jul -  +:Wuyi: toCst3800, Name=stupid, age= -]student [CreateDate=mon Jul -  +:Wuyi: toCst3800, Name=tomcat, age=2333]true

So the Clone method is a shallow clone

Qaq

Code about Java Clone (shallow clone)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.