zoukankan      html  css  js  c++  java
  • 重温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         }
  • 相关阅读:
    分享图片到在线服务
    获取和保存照片
    处理图片(updated)
    简化版“询问用户是否退出”
    捕获高像素照片(updated)
    处理高像素的照片
    加强版照片捕获
    图片拍摄、处理、镜头应用
    Windows Phone 推送通知的第四类推送
    网络通信
  • 原文地址:https://www.cnblogs.com/liyongjian/p/3255609.html
Copyright © 2011-2022 走看看