国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

基于.NET中建構(gòu)子中傳遞子對象的對象詳解_.Net教程

2024-07-10 12:52:31
字體:
供稿:網(wǎng)友

推薦:.NET中STAThread的使用詳解
這個(gè)STA線程模型,在線程內(nèi)加入了訊息幫浦等等機(jī)制,減少開發(fā)人員撰寫窗口程序的工作量

在設(shè)計(jì)對象繼承的時(shí)候,父對象建構(gòu)子會(huì)需要一些參數(shù),這些參數(shù)可以由子對象建構(gòu)子透過base關(guān)鍵詞來提供。
復(fù)制代碼 代碼如下:www.CuoXIn.com

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>()) { }
}
}

但是如果子對象,要使用這個(gè)傳遞給父對象的參數(shù),就需要一點(diǎn)小技巧才能取得了。先來看一開始解決的想法是,先建立子對象的屬性對象,然后再傳遞給父對象。這個(gè)方法很快就失敗,光是編譯就不過了….。對象的建立是先跑建構(gòu)子、然后生出對象。在建構(gòu)子的階段,就要使用對象的屬性,一定是失敗的。
復(fù)制代碼 代碼如下:www.CuoXIn.com

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) { }
}
}

想了一下,換個(gè)角度去解決這個(gè)問題。干脆另外再開一個(gè)子對象的建構(gòu)子,先建立要傳給父對象的對象,然后不直接傳給父對象的建構(gòu)子,而是傳給子對象自己的建構(gòu)子,然后這個(gè)建構(gòu)子在傳遞給父對象。寫到我眼睛都花了,好像繞口令….。直接看程序代碼吧,其實(shí)還蠻簡單就可以完成這個(gè)小小的設(shè)計(jì):

復(fù)制代碼 代碼如下:www.CuoXIn.com

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;
}
}
}

分享:.NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析
本篇文章介紹了,.NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析。需要的朋友參考下

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 同德县| 大冶市| 资兴市| 民勤县| 本溪| 淮阳县| 固始县| 万荣县| 抚顺县| 万山特区| 铅山县| 彝良县| 朝阳区| 华坪县| 富民县| 武宁县| 洪洞县| 陆川县| 凌海市| 渝中区| 嵊泗县| 合作市| 镇平县| 额敏县| 工布江达县| 航空| 垫江县| 西畴县| 桐梓县| 荣昌县| 崇仁县| 大丰市| 太仆寺旗| 土默特右旗| 安陆市| 尤溪县| 北安市| 汉中市| 哈巴河县| 崇仁县| 柘荣县|