public static void InheritanceTest01() { TestCls cls = new TestCls(); TestCls2 cls2 = new TestCls2(); TestCls3 cls3 = new TestCls3(); Console.WriteLine("Test invoking from sub type..."); Console.WriteLine(cls.ToString()); cls.TestAbstract(); cls.TestVirtual(); cls.TestField(); Console.WriteLine(cls2.ToString()); cls2.TestAbstract(); cls2.TestVirtual(); cls2.TestField(); Console.WriteLine(cls3.ToString()); cls3.TestAbstract(); cls3.TestVirtual(); cls3.TestField(); Test01Sub(cls); Test01Sub(cls2); Console.WriteLine("TestCls.TestVal2 = " + cls.TestVal2); ClassInheritanceTest.Test3(cls); ClassInheritanceTest.Test3(cls2); ClassInheritanceTest.Test3(cls3); }
public static void InheritanceTest23() { TestClass cls = new TestClass(); if (cls is InterfaceTest2) { throw new Exception(); } InterfaceTest2 cls2 = cls as InterfaceTest2; if (cls2 is InterfaceTest2) { throw new Exception(); } TestClass3 cls3 = (TestClass3)(cls as InterfaceTest2); if (cls3 != null) { throw new Exception(); } cls2 = new TestCls3(); var cls4 = (TestCls3)cls2; if (cls4 == null) { throw new Exception(); } InterfaceTest2 cls5 = (InterfaceTest2)cls; if (cls5 is InterfaceTest2) { throw new Exception(); } }
public static void InheritanceTest_Interface() { TestCls3 cls3 = new TestCls3(); Console.WriteLine(cls3.ToString()); cls3.TestAbstract(); ((InterfaceTest2)cls3).TestVirtual(); cls3.TestField(); }