When I first came into contact with programming, I really felt that it was abstract, and many times I felt that it was so annoying.
Taking a variable as an example, it is still a simple value, which is easy to understand. Since the idea of object-oriented is popular, variables introduce reference types. Suddenly, many people started to get hurt again.
In fact, for a variable, the variable is just a variable, variables of the reference type only perform some complex processing when using the variable to access the value-from the previous direct value to the memory address indicated by the value to find the corresponding object Value.
For example, I am a subject.
//What is my age?
Myage =24
//What is my weight?
Myweight =70
//What is the color of my coat?
Thecolorofmyjacket = black
This is the value type, which is very simple. We have been involved in elementary school mathematics a long time ago. For example, a = 1, B = 2, and a + B is equal to 3.
Later, after referencing the object-oriented idea. The world has changed, but it has become simple and painful.
For the above three problems, we have changed the representation method:
//(First, I am an object .)
//What is my age?
My. Age =24
//What is my weight?
My. Weight =70
//What is the color of my coat?
//(In nature, I am a thing, and my coat is also a thing, so it needs to be expressed here .)
My. Jacket. Color = black
All right, let's get back to the question.
From the perspective of "Reference", it doesn't matter what value it contains!
The reference is only used to point to a thing. As for how to handle this thing, it depends on the reference type, which is int (Value Type). When it is an integer, if it is an object (reference type), you will know that it is a memory address, and then go to this memory address to retrieve the corresponding type of object.
Many people are obsessed with the mystery of the reference type design and are confused by this mystery. For example, why can a string type variable be used as an object in C #, but can be used as simple as a value type (assign value, etc ).
It is not difficult to understand this problem, but first I need to explain my previous simple demonstration of the object-oriented idea (the example above used to record my age, weight, coat color, and other information using variables ).
We should be familiar with the most important features of object-oriented, often referred to as "encapsulation, inheritance, polymorphism ......" Of course, the most outstanding and most commonly used feature is "encapsulation ". Encapsulation is to pass (or access) a set of correlated data as a whole, a bit like an array or hashtable, but it is more convenient to use than the two, more user-friendly.
Some values are saved in the object, and "attributes" are used. Of course, attributes can also be objects. In this way, there is a very good benefit-or human nature.
For example, my mother's brother's father's wife is my grandmother, expressed in an object-oriented way:
My. Mother. Brother. Father. Wife = My. Grandma
OK. Let's see the benefits of object-oriented! He can relate everything in the world in a very visual way ...... Congratulations ......
(It's a long journey ......)
An object may be large and may have many attributes and many values. In this way, it is certainly impossible to put it on a memory address, instead of a batch of memory units. A batch of memory addresses are directed by a reference. Therefore, the memory is required to store an object on a batch of consecutive addresses, then use this reference to point to the header of this batch of memory addresses (people who have learned C should be familiar with it, and this should be the pointer in C ).
This is the substantial difference between the reference type and the value type.
Now, we will discuss the use of equal signs (Value assignment symbols) for operators.
In most computer languages, the value assignment symbols use equal signs =. An equal sign can assign a data value to a variable (reference), regardless of whether the data is an independent value or an object. For example, if a = 3 or father = new people ("Zhang San"), the two statements indicate that the value of the variable A is equal to the number 3, let the father variable be the person named "Zhang San.
Next, let's take a lookCode:
IntB =4;
IntA = B;
B =5;
After these two statements are executed, the value of variable A is equal to 4, which has no doubt. Over, let's look at another piece of code:
StringS1 ="123";
StringS2 = S1;
S1 ="123456";
In this case, S2 is equal to "123". It's normal. What are your questions? Someone may say, "This is an object! Reference Type object !" But what is the difference?
Here I will reference the object-oriented idea as a simple example:
For example, I found a girlfriend named "123". That is to say, I, this object, my girlfriend, and attributes are the same as the person named "123. OK
Later, a grandson caught up with my girlfriend. At this time, I have not broken up with my girlfriend. At this time, this 123-person-in-laW woman is both my girlfriend and the girlfriend of insun. OK (mourning)
Later, I found another beauty named 123456.
At this time, my girlfriend is 123456, and my grandson's girlfriend is 123. (If it dared to be 123456, the world would be messy, CAO)
Over:
My. Girlfriend =NewPerson ("123");
Sunzi. Girlfriend = My. girlfriend;
My. Girlfriend =NewPerson ("123456");
This example is very clear.
Let's take another example to illustrate the essence of the reference type from another perspective.
For example, my dad used a Nokia mobile phone that my brother (brother-in-law) gave him. Later I saw it too old and bought a new HTC. My brother later lost his cell phone. He wanted to use my dad's cell phone to make a phone call. I bought HTC instead of the Nokia.
UseProgramThe Code is as follows:
P =NewPerson ();
My. Brother. Father = P;
My. Father = P;
My. Brother. Father. Mobile = "Nokia ";
My. Father. Mobile = "HTC ";
Mobile = My. Brother. Father. Mobile;//Mobile = "HTC"
I understand it. Do not explain.
OK, all over.
Other words are not mentioned. This is my personal understanding of the value type and reference type. I don't know if it is absolutely reasonable, but I think that would be the case if I was. I don't want to talk about the deep-seated egg technology, but I hope it will help people who see it ..