Big talk effective Java core combat programming thought of the Monkey King's trick

Source: Internet
Author: User
Tags shallow copy

I do not know whether we remember in the "Journey to the" in the Lotus Hole in the story of the Treasure, is the Monkey King skillfully grabbed the treasures, the gold and Silver Kok King that chapter. What does this story give us? What is the connection between this story and effective Java? Or continue the style of the article, read the code, read the story.

1import static org.junit.assert.*;
2import org.junit.Test;
3
4
5public class Testclone {
6
7 @Test
8 public void Testclone () {
9//west to learn from the road, Golden Horn and silver horn King of the Tang Monk took away
10 Monkey King San =new Monkey King ("Qi Tian San Sun wukong");
11//The Sage hands divine, is about to clean up gold, Silver Kok king.
12-Tianda saint. Acquisition of weapons (New Divine ());
13
14/**//*
15 * This time, King Horn and Silver Kok King heard the big friendly, immediately let the small demon to please out their treasure gourd
16 * Of course, all this is not to deceive the powerful sage. The monkey's hair blew and turned out an old Taoist priest.
17 */
18 Monkey Wang Yin wu Sun Daoshi = (Monkey King) qi Tian San. To become an avatar ();
19 Air Wu Sun Daoshi. renamed ("Goku Sun Daoshi");
20
21/**//*
22 * Old Taoist Little Demon said his gourd more powerful, can put the days are collected, intelligence value only 20 of the small demon saw envy unceasingly, demand exchange gourd.
23 * The old Taoist nature is very happy, changed the gourd, went straight to the Devil Cave, the King of Gold, Silver Kok.
24 */
25 Air Wu Sun Daoshi. Acquire Weapon (New Treasure Gourd ());
26
27//Question 1: What kind of weapon did the Taoist priest take? Taoist monks are cloned from the sage, but not divine, but the treasure gourd?
Assertfalse (The Weapon () instanceof Divine) of San-Tianda;
Asserttrue (Sun Daoshi) the weapon () instanceof treasure gourd);
30
31//Question 2: The sage and the Taoist priest take the same weapon?
Assertsame (Sun Daoshi), the Weapon (), the Weapon () of Tianda saint.
33
34//Question 3: Since the weapon is the same, why is the name different?
Assertequals (Qi Tian san), "Qi Tian San Sun wukong");
Assertequals (Air Wu Sun Daoshi. Name (), "Goku Sun Daoshi");
37
38/**//*
39 * Answer: Monkey King class Inherits Object.clone (), its cloning principle is: If the class each domain contains a primitive type (primitive) value,
40 * or contains a reference to a mutable (final) object, the returned value or object is the same copy, otherwise, if it is a mutable class, the same reference is returned.
41 * Because the Divine class is not a mutable class, and string is, so you should understand why the sage and his clones have different names, but they have the same weapon.
42 *
The Object.clone () is called a shallow copy, or a shallow clone. It corresponds to a deep clone (Deep clone), which means that the class also copies the Mutable object when it is cloned.
44 * See here you should know that in fact this Monkey King is not a reasonable implementation, he should have a deep cloning method.
45 */
46}
47
Class Monkey King implements Cloneable{
private String name;
Private weapons [] weapon=new weapons [1];
51
The public Monkey King (String name) {
This.name=name;
54}
55
56/** *//**
57 * Obtain a Monkey King's superficial clone avatar
* @return
59 */
The public Object becomes an avatar () {
The Object cloneobj=null;
The try {
Cloneobj=clone ();
(Clonenotsupportedexception ex) {
Ex.printstacktrace ();
66}
The return cloneobj;
68}
69
@Override
Protected Object Clone () throws clonenotsupportedexception{
Super.clone ();
73}
74
The public String name () {
The return name;
77}
78
Rename public void (String name)
This.name=name;
81}
82
Arms of the public weapons () {
The return weapon[0];
85}
86
Weapon public void Acquisition Weapon (weapon) {
this.weapon [0] = weapon;
89}
90}
91
A class Weapon {
Public weapons () {
94
95}
96}
97
Class Divine extends Weapon {
Divine public () {
100}
101}
102
Extends class treasure gourd weapon
The Public Treasure Gourd () {
105}
106}
107
108
109}
110

Related Article

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.