测试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 }