博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#基础-第6章:类型和成员基础
阅读量:4585 次
发布时间:2019-06-09

本文共 5039 字,大约阅读时间需要 16 分钟。

本章内容:

  1. 类型的各种成员
  2. 类型的可见性
  3. 成员的可访问性
  4. 静态类
  5. 分部类,结构和接口
  6. 组件、多态和版本控制
// #define 可以按版本执行不同的代码块#define V1//#define V2a//#define V2b//#define V2c#if !DEBUG#pragma warning disable 414, 67#endifusing System;public sealed class SomeType //P(137){                                 //  1    // 嵌套类    private class SomeNestedType { }                            //  2    // 常量    private const Int32 c_SomeConstant = 1;    //readonly 只读                                              //  3    private readonly String m_SomeReadOnlyField = "2";          //  4    //静态可以读/可写字段    private static Int32 s_SomeReadWriteField = 3;              //  5    //类型构造器    static SomeType() { }                                       //  6    // 实例构造器    public SomeType() { }                                       //  7    public SomeType(Int32 x) { }                                //  8    // 实例方法和静态方法    public static void Main1() { }                               //  9    public String InstanceMethod() { return null; }             // 10    // 实例属性    public Int32 SomeProp    {                                     // 11        get { return 0; }                                        // 12        set { }                                                  // 13    }    //实例有参属性(索引器)    public Int32 this[String s]    {                               // 14        get { return 0; }                                        // 15        set { }                                                  // 16    }    // 实例事件    public event EventHandler SomeEvent;                        // 17}internal static class OverridingAccessibility{    class Base    {        //基类虚方法        protected virtual void M() { }    }    class Derived1 : Base    {        //重写基类虚方法        protected override void M() { }    }    class Derived2 : Base    {        protected override void M() { }        public static void Main1() { }    }}//静态类 staitc关键字 永远不需要实例化internal static class DifferentCalls //P(146){       public static void Go()    {        Console.WriteLine();            //调用一个静态方法        Object o = new Object();        o.GetHashCode();                // 调用虚实例方法        o.GetType();                    // 调用非虚实例方法    }}#region Versioning Components With Virtual Methodspublic sealed class VersioningComponentsWithVirtualMethods{    public static void Main()    {        CompanyB.BetterPhone phone = new CompanyB.BetterPhone();        phone.Dial();    }}///#if V1namespace CompanyA//P(150){    public class Phone    {        public void Dial()        {            Console.WriteLine("Phone.Dial");            // 这里执行拨号操作        }    }}namespace CompanyB{    public class BetterPhone : CompanyA.Phone//P(151)    {        //  加了new 关键字 新的Dail方法变得与Phone的Dial方法无关了        new public void Dial()        {            Console.WriteLine("BetterPhone.Dial");            EstablishConnection();            base.Dial();        }        protected virtual void EstablishConnection()        {            Console.WriteLine("BetterPhone.EstablishConnection");            //在这里执行建立连接的操作        }    }}#endif///#if V2a || V2b || V2cnamespace CompanyA {    public class Phone {        public void Dial() {            Console.WriteLine("Phone.Dial");            EstablishConnection();            // Do work to dial the phone here.        }        protected virtual void EstablishConnection() {          Console.WriteLine("Phone.EstablishConnection");           // Do work to establish the connection.        }    }}#endif///#if V2anamespace CompanyB {    public class BetterPhone : CompanyA.Phone {        //保留关键new,指明该方法与基类型的Dail方法没有关系        new public void Dial() {             Console.WriteLine("BetterPhone.Dial");            EstablishConnection();            base.Dial();        }        // 为这个方法添加关键new,指明该方法与基类型的        //  EstablishConnection 方法没有关系.        new protected virtual void EstablishConnection() {            Console.WriteLine("BetterPhone.EstablishConnection");            // 在这里执行建立连接的操作        }    }}#endif#if V2bnamespace CompanyB {    public class BetterPhone : CompanyA.Phone { //P(153)        // 删除Dail方法 (从基类继承Dial)        // 移除关键字'new',将关键字Virtual 修改为 override        // 指明该方法与基类的EstablishConnection方法的关系        protected override void EstablishConnection() {            Console.WriteLine("BetterPhone.EstablishConnection");            // 在这里执行建立连接的操作        }    }}#endif#if V2cnamespace CompanyB {    public class BetterPhone : CompanyA.Phone {        // Keep 'new' to mark this method as having no        // relationship to the base type's Dial method.        new public void Dial() {             Console.WriteLine("BetterPhone.Dial");            EstablishConnection();            base.Dial();        }        // Remove 'new' and change 'virtual' to 'override' to        // mark this method as having a relationship to the base        protected override void EstablishConnection() {            Console.WriteLine("BetterPhone.EstablishConnection");            // Do work to establish the connection.        }    }}#endif#endregion

 

转载于:https://www.cnblogs.com/eric-yuan/p/10223495.html

你可能感兴趣的文章
每天一道Java题[9]
查看>>
结对编程2——单元测试
查看>>
python 函数/列表的应用
查看>>
C#与MES
查看>>
LR接口测试---Java Vuser之jdbc查询(调试前)
查看>>
SQL Server 各版本安装包分享
查看>>
.net项目移植后的虚拟目录的配置问题
查看>>
JSP页面中引入另一个JSP页面
查看>>
Android笔记——活动的生命周期
查看>>
springmvc使用包装的pojo接收商品信息的查询条件
查看>>
【Linux】【Services】【Configuration】puppet
查看>>
poj 1002:487-3279(水题,提高题 / hash)
查看>>
RAC环境上搭建DG
查看>>
OS X Mountain Lion高手进阶
查看>>
初识电流环
查看>>
MySQL每天自动增加分区
查看>>
在线生成坐标值,方便布局使用
查看>>
ab测试工具的使用
查看>>
RTL基本知识:编译命令指定隐性线网类型
查看>>
java中BigDecimal在金融行业中的使用
查看>>