构造器初始化(三):巧用Static方法和base关键字
生活随笔
收集整理的這篇文章主要介紹了
构造器初始化(三):巧用Static方法和base关键字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//如何運行前不知道的值傳遞給 CommandDelimitedFile構造器?答案:使用靜態方法,與base關鍵字一起使用。
namespace InitialConstruct
{class CommandDelimitedFile{public CommandDelimitedFile(string fileName){Console.WriteLine("[CommandDelimitedFile."+"CommaDelimitedFile] file name={0}",fileName);}}enum TableId{Customers,Suppliers,Vendors}class DbTable : CommandDelimitedFile{static string GetFileName(TableId tableId){string fileName;switch (tableId){case TableId.Customers:fileName = "customers.txt";break;case TableId.Suppliers:fileName = "suppliers.txt";break;case TableId.Vendors:fileName = "venders.txt";break;default:throw new ArgumentNullException("DbTable.GetFileName] Could not rsolve table name");}return fileName;}public DbTable(TableId tableId): base(GetFileName(tableId)){Console.WriteLine("[DbTable.DbTable] tableId={0} ", tableId.ToString());}}class Program{static void Main(string[] args){Console.WriteLine("[Main] Instantianting a " + "Customer Table object...");DbTable derived = new DbTable(TableId.Customers); Console.ReadKey();}}
}
總結
以上是生活随笔為你收集整理的构造器初始化(三):巧用Static方法和base关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LINUX下源码包安装mysql
- 下一篇: 地球公转(说一说地球公转的简介)