In JavaScript play inheritance (II), I used the prototype inheritance method to implement JavaScript inheritance. What is the mysteries of prototype. In this articleArticleI will discuss the prototype.
Aside from JavaScript, Let's first look at the common object-oriented language we are familiar. Among the 23 design patterns, you will surely remember that there is a design pattern-the prototype pattern. Let's take a look at the structure:
(Source: http://terrylee.cnblogs.com/archive/2006/01/16/317896.html)
Let's first review the prototype mode: When you see this figure, you will surely notice that this thousands of clone () methods are at the core of the prototype mode. In the prototype mode, you can use a prototype instance to create an object type, and then copy the prototype to create a new object.
In. net, the implementation of this mode is very easy. We only need to implement the icloneable interface:
I still remember the angel saying this in the right line of love's left light,This quality is not guaranteed when everything comes to mass production.. This is a typical prototype: batch production. Why does the quality decrease? The reason is that it adopts replication.
ViewCode:
Class People:Icloneable
{
Private StringName;
Private IntAge;
Private List<String> Friends =New List<String> ();
PublicPeople (StringName,IntAge,Params String[] Names)
{
This. Name = Name;
This. Age = age;
Foreach(StringSInNames)
{
Friends. Add (s );
}
}
Public StringName
{
Get{ReturnName ;}
Set{Name =Value;}
}
Public IntAge
{
Get{ReturnAge ;}
Set{Age =Value;}
}
Public List<String> Friends
{
Get{ReturnFriends ;}
Set{Friends =Value;}
}
Public ObjectClone ()
{
Return This. Memberwiseclone ();
}
Public Override StringTostring ()
{
StringRet ="Name :"+ Name +"Age :"+ Age +"Friends :";
Foreach(StringSInFriends)
{
RET + = S +",";
}
ReturnRET;
}
}
Then we Initialize an object:
PeopleP1 =New People("Windking", 20,"111","222","333","444");
Next, copy an object:
PeopleP2 = (People) P1.clone ();
Output:
Console. Writeline ("P1 :"+ P1.tostring ());
Console. Writeline ("P2 :"+ P2.tostring ());
Okay, now let's change:
P2.name ="Xuan";
P2.age = 22;
P2.friends. Add ("555");
Enter:
Console. Writeline ("P1 :"+ P1.tostring ());
Console. Writeline ("P2 :"+ P2.tostring ());
We can see that P2 changes directly apply to the array elements of the prototype P1. This is just a shortest copy. During the copy, the address of the reference type is copied. That is to say, when two objects point to the same internal reference object, the value of the reference type is as follows. This is also called"This quality is not guaranteed when everything comes to mass production.".
May someone ask: Is string not a reference type? Why is there no prototype affected by P2 changes? For more information, see my next article:C # play string.
What should we do? Can this problem be solved? Yes. We only need to change it to deep replication, which is not a batch production. In fact, I personally think this is not a clone, it is equivalent to encapsulating the task of creating a new object. Let's take a look at this figure to understand deep replication and light replication.
(Source: http://www.cnblogs.com/Terrylee/archive/2006/01/06/312493.html)
Okay. Let's take a look at the copied code:
Other codes are the same as above. The difference is that the copied code:
Public ObjectClone ()
{
List<String> Clonefriends =New List<String> (Friends );
String[] Clonearrayfriends = clonefriends. toarray ();
// Actually, It is not mysterious, but it encapsulates the initialization object.
Return New People(This. Name,This. Age, clonearrayfriends );
}
Test:
P2 changes do not affect the prototype P1.
well, it was originally about JavaScript prototype, but it turned out to be a page prototype. Too many. I will discuss prototype of JavaScript below. Stay tuned.