• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

永远有李

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

重温new和override关键字

测试1
定义类:

1
interface ITestClass 2 { 3 string UserName { get; set; } 4 } 5 6 class TestParentClas : ITestClass 7 { 8 public string UserName 9 { 10 get; 11 set; 12 } 13 } 14 15 class TestChildClass1 : TestParentClas 16 { 17 public string UserName 18 { 19 get; 20 set; 21 } 22 23 }
测试结果1
 1         private void TestU()
 2         {
 3             TestChildClass1 t1 = new TestChildClass1();
 4             t1.UserName = "Child";
 5             
 6             ITestClass t2 = t1;
 7             string a1 = t2.UserName;//a1为null
 8 
 9             TestParentClas t3 = t1;
10             string a2 = t3.UserName;//a2为null
11         }

测试2

    class TestChildClass2 : TestParentClas
    {
        public override string UserName
        {
            get;
            set;
        }
    }

编译错误:cannot override inherited member 'TestParentClas.UserName.set' because it is not marked virtual, abstract, or override

1     class TestParentClas : ITestClass
2     {
3         public virtual string UserName
4         {
5             get;
6             set;
7         }
8     }

测试2结果:

 1         private void TestU()
 2         {
 3             TestChildClass2 t1 = new TestChildClass2();
 4             t1.UserName = "Child";
 5             
 6             ITestClass t2 = t1;
 7             string a1 = t2.UserName;//a1为Child
 8 
 9             TestParentClas t3 = t1;
10             string a2 = t3.UserName;//a2为Child
11         }

 

 

posted on 2013-08-13 17:56  永远有李  阅读(190)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3