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         }
  • 相关阅读:
    58.与人相处的艺术
    26.随时随俗
    24.心平气和
    61.扶树与扶人
    47.非要坚持下去吗
    42.有“舍”才有“得”
    62.离阳光只有五十米
    49.用微笑把痛苦埋葬
    60.换个角度,你便是赢家
    35.忍是大智、大勇、大福
  • 原文地址:https://www.cnblogs.com/liyongjian/p/3255609.html
Copyright © 2011-2022 走看看