Examples of the instance (C #)

zhaozj2021-02-16  48

Examples of the instance (C #)

I haven't posted articles for a long time, I am also interesting, I suddenly feel some categories in C #, why, is its constructor?

And usually these classes often exist in one example. So, I did an experimental results as follows:

The summary description of this example: use system; namespace consoleapplication1 {///

/// Class1. /// class class1 {/// /// The primary entry point of the application. /// [static "static void main (string [] args) {// // Todo: Add code here to start the application Class2 C1 = C1 = Class2.init; Class2 C2 = Class2.init; C2 .refdis ();

Class3 C3 = new class3 (); Class3 C4 = C3; C4.Refdis ();

Class3 c5 = new class3 (); c5.refdis ();

CONSOLE.READLINE ();}} class class2 {static int REF_2 = 0; private class2 () { Ref_2;} public static readonly class2 init = new class2 (); public void refDis () {Console.Writeline ("Class2 Quote: " Ref_2);}}} class class3 {static int REF_3 = 0; public class3 () { ref_3;} public void refDis () {Console.writeline (" Class3 reference: " ref_3); }

}

Output results: Class2 reference: 1class3 reference: 1class3 reference: 2

I saw the result of the above maybe you will be a bit surprised, maybe it will not, isn't it? In many cases, we all try

How much is it to get an instance of a class? The approach that is usually thought is to instance counters. Yes, use a static variable

To do an instance counter is right. However, sometimes we don't want, there are too many instances.

Such as: DataRead it has only one. So what should we do? Did you see the results of my experiment above?

Ah, what did you find? Public Static Readonly class2 init = new class2 ();

Is it very interested in this sentence? Do you have any objection? Remove that STATIC? So well, if you are willing.

Like this: public retadonly class2 init = new class2 (); let us see what will happen, ah

I saw it. There is no way to instantiate the operation. Ha ha. . . Don't worry, let me further explain that static members are

Placing in a static memory is configured in the initial stage of the program loading. So appreciating, the value of init is in fact

At first, NEW gave it. Then it holds a reference to this object. Let's take a look at these two clas2 c1 = class2.init;

Class2 C2 = Class2.init;

It seems that there is nothing special, yeah, according to my saying, this is just a reference. So instance counters

Always one, because I said, is the beginning of the program, complete the configuration. Then the following sentences are the best

instruction of:

// Transfer reference does not instance new object, so the instance counter is still 1 Class3 C3 = New class3 (); Class3 C4 = C3; c4.refdis ();

// Assign a new instance once, the value of the instance counter plus 1. That is, 2 Class3 C5 = New class3 (); c5.refdis ();

Everything is the same as the imagination. I think this is life. Enjoy, there is no doubt that this is right. So, come back, goodbye (finish)

转载请注明原文地址:https://www.9cbs.com/read-27446.html

New Post(0)