I may ask some questions about the constructor during the interview ~, Constructor interview

Source: Internet
Author: User
Tags reflector

I may ask some questions about the constructor during the interview ~, Constructor interview

 

The constructor is also a frequently asked question by the interviewer. We know that the initialization of a class depends on it. Below are a few small questions.

Q: I can see that there is usually a BasePage page in a Web project, where the constructor performs permission verification,

Why.

A: Now that this is done, the designer will surely understand that in the instance constructor, the parent class constructor is executed before the child class, so this can be executed first.

To do a lot of interesting things, such as permission verification, someone may ask why it should be executed before the subclass. As I said just now, the constructor is used

Initialize the status of this class. This is also called the principle of "scanning the house first and then inviting guests ~, Then, we can trace back to the Object ctor. Now, let's answer.

First, you can simplify the problem so that you don't need a web project to demonstrate it.

 1     public class Program 2     { 3         static void Main(string[] args) 4         { 5             var b = new Bird(); 6  7             Console.Read(); 8         } 9     }10 11     public class Animal12     {13         public Animal()14         {15             Console.WriteLine("running first... i'm animal, all must be running after me.");16         }17     }18 19     public class Bird : Animal20     {21         public Bird()22         {23             Console.WriteLine("the next... i'm a cute bird.");24         }25     }

It can be seen that Animal is executed first in Bird, and someone may ask, who calls Animal? Of course it is the Object. Let's take a look at IL:

 

Q: Since you said that the constructor is used to initialize the initial state of the class, I have a series

Json: {"Name": "smart", "Age": 2 }. The object class is as follows. After my json serialization, Age =?

 1     [Serializable] 2     public class Bird 3     { 4         private string name = "smart"; 5  6         public string Name 7         { 8             get { return name; } 9             set { name = value; }10         }11 12         private int age = 2;13 14         public int Age15         {16             get { return age; }17             set { age = value; }18         }19 20         public Bird()21         {22             Age = 5;23         }24     }

 

A: Actually, the key to this problem is whether the constructor will be called during deserialization. Let's first look at the source code through Reflector and find that there is no code.

It's a little strange. I just used ILSpy to decompile the code, and there is no code. It is undeniable that there must be code execution at the underlying layer, either the decompilation fails or the clr is used.

For other methods, we cannot see the source code. If you have any suggestions, please help me. Thank you.

 

Reflector:

Q: I know that the reference type can be a constructor. Why cannot I define a non-argument constructor for the value type?

A: This is A good question. The compiler will not call A value-Type constructor based on performance considerations, even if you force A new one, it will not execute

 1 namespace Sample 2 { 3     public class Program 4     { 5         static void Main(string[] args) 6         { 7             Point point = new Point(); 8         } 9     }10 11     public struct Point12     {13         public int Age;14     }15 }

However, unless you explicitly define a constructor with parameters, and the value type has a feature that initialization is required before reading, the compilation will fail.

 

Q: Can I create a singleton in a class constructor?

A: whether it can be done or not depends on the features of the class constructor. We know that the class constructor is the same as the instance constructor. It is used to initialize static fields and threads.

The internal locks are performed when the class constructor is used. Therefore, when multiple threads access the same time, only one thread executes the class constructor.

Yes.

 


Several constructor Problems

Class Desk {
Desk () {weight = high = width = length = 0 ;}
Public:
Desk: Desk (int ww, int l, int w, int h ){
Weight = ww; high = 1;
Width = w; length = h;
}
Void setWeight (int w) {weight = w ;}
Private:
Int weight, length, width, high;
};
Void main (){
Desk d (2, 3, 5 );
// Desk a [10]; // Q1. why is this error? ------ Because your default constructor is private
Desk * pb; // yes, it is because it only defines a pointer and has not initialized the object. pb now points to 0 xcccc, Which is null.
// Desk d; // This is because your default constructor is private.
Pb = new Desk (,); // Q3. Why Is this correct? ------ Here, the constructor with parameters you defined is called, which is public.
}
A class can have any number of constructors. Your Desk: can be completely removed. This is required if the class is not defined.
When defining an object, the default no-parameter constructor is automatically called without parameters (even if you do not write the compiler yourself, a default value will be added)
An array of objects means that the elements of an array are objects.
Good learning... You can add a breakpoint at the entry of several constructors, And you can clearly see which constructor is called in each case.

C ++ hopes to give me some advice on a small problem with constructor.

Objects of the base class cannot be instantiated directly because they are declared to be protected and invisible to the outside.
If Base b1 ('A') is written, an error is returned.
The derived class executes the constructors of the base class before construction, but the protected constructors are visible in the derived class. Therefore, the object of the derived class can be instantiated.

If it is a private constructor, it cannot be called in any way. This is usually used for example, to declare a copy constructor as a private class that does not want to be copied.

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.