public void Test01() { Shape s = new Circle(); s.MethodVirtual(); s.Method(); Circle c = new Circle(); c.MethodVirtual(); c.Method(); s = new Triangle(); s.MethodVirtual(); s.Method(); Triangle t1 = new Triangle(); t1.MethodVirtual(); t1.Method(); s = new Diamond(); s.MethodVirtual(); s.Method(); Diamond d = new Diamond(); d.MethodVirtual(); d.Method(); }
private void Form1_Load(object sender, EventArgs e) { //第一個 Shape circleA = new Circle(); circleA.MethodVirtual(); circleA.Method(); Circle circleB = new Circle(); circleA.MethodVirtual(); circleA.Method(); //circleA 、 B 都是以下輸出 //Circle Override MethodVirtual 成功 //Base Method Call Shape triangleA = new Circle(); triangleA.MethodVirtual(); triangleA.Method(); Triangle triangleB = new Triangle(); triangleB.MethodVirtual(); triangleB.Method(); //triangleA 輸出以下: //Circle Override MethodVirtual 成功 //Base Method Call //triangleB 輸出以下: (完全覆蓋過去) //triangle new MethodVirtual //triangle new Method Shape diamondA = new Diamond(); diamondA.MethodVirtual(); diamondA.Method(); Diamond diamondB = new Diamond(); diamondB.MethodVirtual(); diamondB.Method(); //diamond A: //base MethodVirtual call //Base Method Call //diamond B: //Diamond default MethodVirtual //Diamond default Method }
private static void TestDerive() { Console.WriteLine("TestDerive\tStart"); Circle circle = new Circle(); Rectangle rectangle = new Rectangle(); Triangle triangel = new Triangle(); Diamond diamond = new Diamond(); circle.MethodVirtual(); circle.Method(); rectangle.MethodVirtual(); rectangle.Method(); triangel.MethodVirtual(); triangel.Method(); diamond.MethodVirtual(); diamond.Method(); Console.WriteLine("TestShape\tEnd\n"); }