using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{ public class BaseClass { ////// 常量 /// public const int Number = 10; ////// 属性 /// public int ID { get; set; } ////// 字段 /// public string Name; ////// 方法 /// ///public void SayHello() { Console.WriteLine("我是无返回类型的方法"); } /// /// 索引 /// /// ///public int this[int index] { get { switch (index) { case 0: return 0; break; case 1: return 1; break; case 2: return 2; break; case 3: return 3; break; default: return 10000; break; } } set { switch (index) { case 0: ID = value; break; case 1: ID = value; break; case 2: ID = value; break; case 3: ID = value; break; default: ID = value; break; } } } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{ public class MyClass : BaseClass { ////// 常量 /// new public const int Number = 10; ////// 属性 /// new public int ID { get; set; } ////// 字段 /// public string Name; ////// 方法 /// ///public string SayHello() { string str = "hello"; return str; } /// /// 索引 /// /// ///public int this[int index] { get { switch (index) { case 0: return 0; break; case 1: return 1; break; case 2: return 2; break; case 3: return 3; break; default: return 10000; break; } } set { switch (index) { case 0: ID = value; break; case 1: ID = value; break; case 2: ID = value; break; case 3: ID = value; break; default: ID = value; break; } } } }}
使用new关键字隐藏基类成员