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

首頁 > 編程 > .NET > 正文

第十三章 枚舉類型與位標(biāo)記[《.net框架程序設(shè)計》讀書筆記]

2024-07-10 12:59:32
字體:
供稿:網(wǎng)友
  • 網(wǎng)站運營seo文章大全
  • 提供全面的站長運營經(jīng)驗及seo技術(shù)!
  • 第十三章 枚舉類型與位標(biāo)記

    一、 枚舉類型

    1、 使用枚舉類型的理由:

    l 枚舉類型是得程序更容易編寫、閱讀、維護,在代碼中使用符號名稱代替數(shù)字是程序設(shè)計的一貫主張。

    l 強類型的,便于類型檢驗

    2、 注意事項:

    l 枚舉類型繼承自system.enum,system.enum又繼承自system.valurtype

    l 枚舉類型不能定義方法、屬性、事件

    l 枚舉類型為常數(shù)而非只讀字段,因此可能引入版本問題(見第八章的相關(guān)討論)

    l 將枚舉類型與引用它的類型定義在同一層次上,可減少代碼錄入的工作量



    3、 system.enum中方法的應(yīng)用:

    l public static type getunderlyingtype(type enumtype);
    獲取用于保存枚舉類型實例值得基礎(chǔ)類型。聲明某枚舉類型使用的基礎(chǔ)類型語法如下:
    enum human : byte
    {
    male,
    female
    }
    則調(diào)用上述方法enum.getunderlyingtype(typeof(human));將返回system.byte;
    l public override string tostring();
    public string tostring(string); //參數(shù)為格式字符串
    public static string format(type enumtype,object value,string format);
    //value – 要轉(zhuǎn)換的值,format – 格式字符串(g,g,x,x,d,d,f,f)
    l public static array getvalues(type enumtype);
    獲取枚舉中常數(shù)值的數(shù)組
    l public static string getname(type enumtype,object value);
    在指定枚舉中檢索具有指定值的常數(shù)的名稱
    l public static string[] getnames(type enumtype);
    檢索指定枚舉中常數(shù)名稱的數(shù)組。
    l public static object parse(type, string);
    public static object parse(type, string, bool);
    將一個或多個枚舉常數(shù)的名稱或數(shù)字值的字符串表示轉(zhuǎn)換成等效的枚舉對象
    l public static bool isdefined(type enumtype,object value);
    返回指定枚舉中是否存在具有指定值的常數(shù)的指示,value為常數(shù)的值或名稱
    l 系列toobject方法
    返回設(shè)置為指定值的、指定枚舉類型的實例
    二、 位標(biāo)記

    l 使用system.flagsattributes定制特性,使得tostring或format方法可以查找枚舉數(shù)值中的每個匹配符號,將它們連接為一個字符串,并用逗號分開;parse方法可用該特性拆分字符串并得到復(fù)合的枚舉類型

    l 使用格式字符串f或f 也有同樣的效果

    下面的示例說明上述情況

    using system;



    [flags] //定制特性

    public enum human : byte //定制基本類型

    {

    male = 0x01,

    female = 0x10

    }



    public class enumtest

    {

    public static void main()

    {

    human human = human.male | human.female; //人妖?



    console.writeline(human.tostring()); //使用flags定制特性的情況

    //console.writeline(human.tostring("f")); //沒有使用flags定制特性的情況



    console.writeline(enum.format(typeof(human), human, "g"));//使用flags定制特性的情況

    //console.writeline(enum.format(typeof(human), human, "f"));//沒有使用flags定制特性的情況



    human = (human)enum.parse(typeof(human), "17");

    console.writeline(human.tostring()); //使用flags定制特性的情況

    //console.writeline(human.tostring("f")); //沒有使用flags定制特性的情況

    }

    }



    /*運行結(jié)果

    male, female

    male, female

    male, female

    */

    注:上述程序中的注釋為不使用flags特性時的語法
    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 青州市| 嘉鱼县| 通辽市| 朔州市| 静安区| 平乐县| 项城市| 白城市| 宁陵县| 萨嘎县| 岳池县| 许昌市| 乌鲁木齐市| 石台县| 重庆市| 万荣县| 泰顺县| 北海市| 清徐县| 德昌县| 郯城县| 乡宁县| 资阳市| 天台县| 甘洛县| 新河县| 忻城县| 宜州市| 曲靖市| 赫章县| 丹巴县| 郑州市| 犍为县| 云阳县| 平南县| 突泉县| 商城县| 沙坪坝区| 民勤县| 南京市| 乐清市|