C# 索引器 扩展方法 new使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new_csharp
{
    /*
public class BaseC
{
    public int x;
    public void Invoke() { }
}
public class DerivedC : BaseC
{
    new public void Invoke() { }
}
在此示例中,DerivedC.Invoke 隐藏了 BaseC.Invoke。 字段 x 不受影响,因为它没有被类似名称的字段隐藏。

通过继承隐藏名称采用下列形式之一:

引入类或结构中的常数、指定、属性或类型隐藏具有相同名称的所有基类成员。

引入类或结构中的方法隐藏基类中具有相同名称的属性、字段和类型。 同时也隐藏具有相同签名的所有基类方法。

引入类或结构中的索引器将隐藏具有相同名称的所有基类索引器。

对同一成员同时使用 new 和 override 是错误的做法,因为这两个修饰符的含义互斥。 new 修饰符会用同样的名称创建一个新成员并使原始成员变为隐藏的。 override 修饰符会扩展继承成员的实现。

在不隐藏继承成员的声明中使用 new 修饰符将会生成警告。

示例

在该例中,基类 BaseC 和派生类 DerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。 该示例演示了 new 修饰符的用法。 另外还演示了如何使用完全限定名访问基类的隐藏成员。

 */

    class Program
    {
 
    
        static void Main(string[] args)
        {

            B b = new B();
            Console.WriteLine(b.m_x.ToString());//输出为5;
            Console.WriteLine(b.X.ToString());//输出为5;
         
            baseA a = b;
            Console.WriteLine(a.m_x.ToString());//输出为4;
            Console.WriteLine(a.X.ToString());
            a.sayHello();

            b["jack"] = 100;
            b["tom"] = 99;
            b["stevfen"] = 88;
            Console.WriteLine(b["jack"].ToString());//输出100
            Console.WriteLine(b["jk"].ToString());//输出-1;
            b.syschinese();// 输出 5: 你好
            Console.ReadLine();


        }
    }
    public class baseA
    {
        class sj
        {

        }
        public int m_x = 4;
        public int X
        {
            set
            {
                m_x = value;
            }
            get
            {
                return m_x;
            }
        }
        public void sayHello()
        {
            Console.WriteLine("hello A");
        }

    }

    public class B : baseA
    {
        Dictionary<string, int> m_dic = new Dictionary<string, int>();
        new public int m_x = 5;
        public int X //没有new的时候会出现警告
        {
            set
            {
                m_x = value;
            }
            get
            {
                return m_x;
            }
        }
        new public void sayHello()
        {
            Console.WriteLine("hello B");
        }
        //索引器相当于C++的运算符[]的重载
        public int this[string strname]
        {
            get
            {
                int sorce;
                if (m_dic.TryGetValue(strname, out sorce))
                {
                    return sorce;
                }
                return -1;
            }
            set
            {
                int sorce;
                if (m_dic.TryGetValue(strname, out sorce))
                {
                    m_dic[strname] = value;
                }
                else
                {
                    m_dic.Add(strname, value);
                }
            }
        }

    }
    //扩展方法 1 在静态类中
    //2 静态方法 参数中 首先是 this 类名 类对象
    //扩展方法不是类的成员函数 但是可以像成员函数一样去使用。类对象只能使用public 函数
    public static class hc
    {
        public static void syschinese(this B Bobj)
        {
            Console.WriteLine(Bobj.m_x.ToString() + ": 你好");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值