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         }
  • 相关阅读:
    51nod 1051【基础】
    HDU5971【瞎搞】
    Lightoj1018 【状压DP】
    HDU2604【矩阵快速幂】
    HDU1501【简单DP】
    HDU3555【数位DP】
    Lightoj1037【状压DP】
    51nod 1099【贪心】
    HDU5950【矩阵快速幂】
    51nod 1049【经典】
  • 原文地址:https://www.cnblogs.com/liyongjian/p/3255609.html
Copyright © 2011-2022 走看看