C # basic collection-Constructor

Source: Internet
Author: User

 

Recently, I want to learn about the new features of C #4.0. After all, I usually use the 2.0 syntax for projects --.

Taking the seventh edition of this advanced tutorial, there is love in the new book, and you are always used to cutting the contents and citation first. I think about it, but I think it should be a review and study.

Although I have understood the constructor before, I feel it is necessary to take note of this process after reading it. It is to deepen the memory and memo.

What is important to this knowledge point is to understand the creation process of the constructor.

Example:

1 class people

2 {

3 protected string name;

4

5}

6

7 class mm: people

8 {

9 private int _ age;

10

11 public void say ()

12 {

13 Console. Write (this. _ age + "," + base. name );

14}

15

16}

17

18 class Program

19 {

20 static void Main (string [] args)

21 {

22mm mmm = new mm ();

23 mmm. say (); // The result is: 0 ,;

24 Console. ReadLine ();

25}

26}

The example is simple and the result is easy to guess. Modify the column:

1 class people

2 {

3 protected string _ name;

4

5 private people ()

6 {

7

8}

9}

10

11 class mm: people

12 {

13 private int _ age;

14

15 public void say ()

16 {

17 Console. Write (this. _ age + "," + base. _ name );

18}

19

20}

In this case, a compilation error occurs: "ConstructorTest. people. people ()" is not accessible because it is restricted by the protection level.

The reason is that the 5th rows are private to the constructor.

When I understand the initialization process of the class, I know why I want to get these two columns. In fact, the duplicate error information can also be seen.

I want to clarify the initialization process of objects in the inherited multi-level structure.

You can see everything clearly:

 

When we execute

1mm mmm = new mm ();

As we know, constructors are used to initialize their own data variables when creating instantiated objects. In the above example, MM calls the constructor of its parent class People to initialize People when initializing itself, people needs to call the constructor of their parent class, so that they can keep looking up until the "source of everything" Object. In fact, this is also quite understandable. This is a class society. The younger brother can give food to his second child, and the second child cannot eat it to the boss. The Obejct is the boss, first, he is full (initialized by himself), and then he is giving the rest to the second child (initialized) and the second child to the younger brother (initialized ).

This process is easy to understand, but there are several points worth attention in this process.

1. when the constructor of the parent class is called and the parent class is not explicitly adjustable, the compiler will create a default constructor for it (No parameter null operation ), the function is to assign a null value to the variable. As in the first example, the int type is 0, and the string type is null.

2. During the call, if the parent class explicitly writes the constructor, the access level of the constructor must be accessible to sub-classes (I .e., not private ). In the second example, when the initial MM is removed, the compiler will create a default constructor for MM because MM does not have an explicit constructor, then he tries to call the non-argument constructor of the parent class. At this time, because the parent class already has an explicit constructor, he does not create it, but this constructor is private, the subclass cannot be called, so the creation process fails here, that is, an error occurs.

3. By default, the following statements are equivalent, but useful for understanding.

1 class people

2 {

3 protected string _ name;

4 // use the base () Description to call its parent class Constructor

5 // empty constructor write not write: base () is the same

6 // but to initialize some variables in the parent class, pass the parameter to the parent class through: base (xxx ).

7 public people ()

8: base ()

9 {

10

11}

12

13}

14

15 class mm: people

16 {

17 private int _ age;

18

19 public mm ()

20: base ()

21 {

22}

23

24 public void say ()

25 {

26 Console. Write (this. _ age + "," + base. _ name );

27}

28

29}

The creation process is like this. After understanding this, you can control the initialization process of the inherited multi-layer structure.

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.