Introduction to the use of objects in object-oriented languages

Source: Internet
Author: User

To avoid misleading people in this article, we first declare that it is of little significance to explore these features in object-oriented languages, but they can help us better understand the features of. NET language. This document uses C # as an example to describe how to clone (Shortest copy) in. net ).

This discussion is caused by cooperative development. At that time, the New Keyword was used in the dbhelper layer to create a database table object (datatable), while the New Keyword was not added when the data access layer (DAL) received the database table object, it only declares an "object variable": datatable dt of the datatable type, but does not declare this: datatable dt = new datatable (); Is this correct?

Let's not talk about it. Let's talk about the principle: everyone on Earth knows that to instantiate an object, you must use the keyword new, for example, datatable dt = new datatable ();. This new function allocates memory. When the compiler reads the keyword "new", it opens up a space in the memory to place the class instance, in DT, only the first address of the memory space is stored. It is not an object. How can an object be stored in a variable? In fact, this dt is of the int32 type by default. It only stores an address and can also be called a pointer. However, if we do not use new, we can directly use datatable DT; to declare a variable whose type is datatable, which means this variable can accommodate the first address of the datatable instance, however, DT is only an int32 variable. So similar:

 

Datatable dt1 = new datatable (); // instantiate class datatable dt2; // defines an object, but does not instantiate dt2 = dt1; // first address of the Instance stored in dt1, pass to dt2

 

In this way, we found that both dt1 and dt2 can complete the function perfectly. On the surface, there are two instances, but there is actually only one instance! Dt2 only references the instance address in dt1! Dt1 and dt2 use the same memory address for the same instance. Therefore, they interact with each other. If dt1 is forcibly destroyed, dt2 cannot be used normally. (If you are interested, you can try it by yourself. Here is not an example ). In fact, we can say that the object refers to a certain area in the memory, rather than the "object variable" You define ".


If you have learned C language, you can easily understand what I mentioned above. However, some people say that this is actually a clone in. Net-shallow replication. This is a big mistake! At that time, I did not think carefully, so I immediately agreed with it, but later I thought carefully and found that this was basically two concepts. Definition of clone (Shortest copy):"Create a superficial copy of the current object by creating a new object and then copying the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not copied.". Now, the reader can find that cloning actually creates an "incomplete object", which is not the same as the reference address mentioned above. First, a new object is created, that is, the memory space has been opened up. Then, the fields of the numeric type in the cloned object (stack data, it can be simply understood as a local variable of the numeric type) copied to the memory space of the new object. For the reference type field (that is, the object) in the cloned object ), only copying references (copying the memory address) does not open up memory space. This principle is consistent with the above description. To put it simply:Cloned and copied numeric data and referenced objects. The method mentioned above is completely referenced..


To be more convincingCodeThe running result describes everything:

 

Clstestb class:

Class clstestb {private string strbyval; // numeric field Public String byval {get {return strbyval ;}set {strbyval = value ;}}}

 

The clstesta class implements the clone interface:

 

 
Class clstesta: icloneable {private string strbyval; // numeric field private clstestb = new clstestb (); // reference field // byval attribute access numeric field Public String byval {get {return strbyval;} set {strbyval = value ;}} // access the reference field Public String byref {get {return clstestb. byval;} set {clstestb. byval = value ;}// implement the clone interface public object clone () {return (object) This. memberwiseclone ();}}

Sample Code for full reference:

 

 

 
Clstesta clstest1 = new clstesta (); // create a new clstesta class instance clstesta clstest2; // define the clstesta class "object variable" clstest1.byval = "0 "; // set the initial value of the clstest1 Value Attribute clstest1.byref = "0"; // set the initial value of the clstest1 reference attribute clstest2 = clstest1; // clstest2 completely references the address pointed to by clstest1, no new instance is created. clstest1.byval = "byval"; // The initial value of clstest1 is changed. clstest1.byref = "byref"; // The initial value of clstest1's reference attribute is changed. MessageBox. show (clstest2.byval + "|" + clstest2.byref );

 

 

The output result of this Code is byval | byref. Analysis: After clstest2 references clstest1, clstest1 changes the numeric and reference attributes, and the corresponding attributes of clstest2 also change, indicating whether it is a numeric field or a reference field, clstest2 fully references clstest1 and clstest2.


Clone sample code:

 

 
Clstesta clstest1 = new clstesta (); // create a new clstesta class instance clstesta clstest2; // define the clstesta class "object variable" clstest1.byval = "0 "; // set the initial value of the clstest1 Value Attribute clstest1.byref = "0"; // set the initial value of the clstest1 reference attribute clstest2 = (clstesta) clstest1.clone (); // clstest1 assigns clstest2 clstest1.byval = "byval" by cloning; // Changes the initial value of clstest1's numerical attribute clstest1.byref = "byref"; // Changes the initial value of clstest1's reference attribute MessageBox. show (clstest2.byval + "|" + clstest2.byref );

 

The output result of this Code is: 0 | byref. Analysis: clstest2 is cloned from clstest1, clstest2 is a new instance, and the numeric attribute in clstest2 is copied from clstest1, which is no longer affected by clstest1; the referenced attribute in clstest2 directly references clstest1. Therefore, when the referenced attribute in clstest1 changes, the corresponding attribute in clstest2 is affected.

To sum up, we can even write as follows:

 

Datatable dt1 = new datatable (); // instantiate class datatable dt2; // defines an object, but does not instantiate datatable dt3; // defines an object, but does not instantiate dt2 = dt1; // pass the first address of the Instance stored in dt1 to dt2dt3 = dt2; // send the first address of the Instance stored in dt2 to dt3

 

 

This method is completely feasible! If it is instantiated once, it can be directly referenced even if it is not instantiated. Some people may wonder: what if dt1 is destroyed when we use dt3? You can rest assured that the. NET hosting mechanism will not release a referenced memory address. Any "object" in dt1, dt2, and dt3 is in use, and the address they direct to will not be released. (If you want to know why it will not be released, see Windows core programming.)

Of course, we need to develop a good habit of adding new to all instantiated objects. After all, we use an object-oriented language instead of the underlying language, so we don't have to worry too much about these issues. Remember to add a new when instantiating an object to ensure there will be no problems!

 

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.