In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to construct sub-objects in .NET. It is very detailed and has a certain reference value. If you are interested, you must read it.
When designing object inheritance, the parent object constructor requires parameters that can be provided by the child object constructor through the base keyword.
The copy code is as follows:
Namespace Test001
{
Public class ParentClass
{
/ / Constructors
Public ParentClass (IEnumerable dataCollection)
{
This.DataCollection = dataCollection
}
/ / Properties
Public IEnumerable DataCollection {get; private set;}
}
Public class ChildClass: ParentClass
{
/ / Constructors
Public ChildClass (): base (new List ()) {}
}
}
But in the case of a child object, it takes a little trick to use the parameter passed to the parent object. The idea to start with is to create a property object for the child object and then pass it to the parent object. This method fails very quickly, and compiling is nothing more than... . . The establishment of an object is to build a constructor first, and then produce an object. In the construction stage, you have to use the properties of the object, which must be a failure.
The copy code is as follows:
Namespace Test002
{
Public class ParentClass
{
/ / Constructors
Public ParentClass (IEnumerable dataCollection)
{
This.DataCollection = dataCollection
}
/ / Properties
Public IEnumerable DataCollection {get; private set;}
}
Public class ChildClass: ParentClass
{
/ / Fields
Private readonly List _ dataCollection = new List ()
/ / Constructors
Private ChildClass (): base (_ dataCollection) {}
}
}
Think about it for a while and solve the problem from a different point of view. Simply open another constructor of the child object, first create the object to be passed to the parent object, and then pass it not directly to the constructor child of the parent object, but to the constructor child of the child object itself, and then the constructor is passing to the parent object. I wrote that my eyes were dazzled, like a tongue twister. . . Look directly at the program code, in fact, it is quite simple to complete this small design:
The copy code is as follows:
Namespace Test003
{
Public class ParentClass
{
/ / Constructors
Public ParentClass (IEnumerable dataCollection)
{
This.DataCollection = dataCollection
}
/ / Properties
Public IEnumerable DataCollection {get; private set;}
}
Public class ChildClass: ParentClass
{
/ / Fields
Private readonly List _ dataCollection = null
/ / Constructors
Public ChildClass (): this (new List ()) {}
Private ChildClass (List dataCollection)
: base (dataCollection)
{
_ dataCollection = dataCollection
}
}
}
These are all the contents of the article "how to construct objects that pass sub-objects in. Net". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.