Detailed explanation of the objects that transmit Sub-objects in the constructor Based on. NET

Source: Internet
Author: User

When designing Object Inheritance, the parent Object Construction sub-requires some parameters. These parameters can be provided by the sub-Object Construction sub-object through the base keyword.
Copy codeThe Code is as follows:
Namespace Test001
{
Public class ParentClass
{
// Constructors
Public ParentClass (IEnumerable <string> dataCollection)
{
This. DataCollection = dataCollection;
}

 
// Properties
Public IEnumerable <string> DataCollection {get; private set ;}
}

Public class ChildClass: ParentClass
{
// Constructors
Public ChildClass (): base (new List <string> ()){}
}
}

However, if you want to use the parameter passed to the parent object as a sub-object, you need a little trick to get it. The first solution is to create a property object for the sub-object and pass it to the parent object. This method will soon fail, and compilation alone will fail ..... Object creation is to first run the constructor and then produce the object. In the sub-construction stage, the attributes of the object must be used, which must fail.
Copy codeThe Code is as follows:
Namespace Test002
{
Public class ParentClass
{
// Constructors
Public ParentClass (IEnumerable <string> dataCollection)
{
This. DataCollection = dataCollection;
}

 
// Properties
Public IEnumerable <string> DataCollection {get; private set ;}
}

Public class ChildClass: ParentClass
{
// Fields
Private readonly List <string> _ dataCollection = new List <string> ();

 
// Constructors
Private ChildClass (): base (_ dataCollection ){}
}
}

Think about it and try to solve it from another angle. Simply create another sub-Object Construction sub-object, first create the object to be passed to the parent object, and then not directly pass to the parent Object Construction sub-object, but to the sub-object's own construction sub-object, then this constructor is passed to the parent object. I spent all my eyes, like a tongue twister ..... Let's look at the program code. In fact, it's quite simple to complete this small design:
Copy codeThe Code is as follows:
Namespace Test003
{
Public class ParentClass
{
// Constructors
Public ParentClass (IEnumerable <string> dataCollection)
{
This. DataCollection = dataCollection;
}

 
// Properties
Public IEnumerable <string> DataCollection {get; private set ;}
}

Public class ChildClass: ParentClass
{
// Fields
Private readonly List <string> _ dataCollection = null;

 
// Constructors
Public ChildClass (): this (new List <string> ()){}

Private ChildClass (List <string> dataCollection)
: Base (dataCollection)
{
_ DataCollection = dataCollection;
}
}
}

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.