定义一基类:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Demo
- {
- public class Base
- {
- public virtual double Operation(double x, double y)
- {
- return x+y;
- }
- }
- }
第一层派生类Invoking中Override该Virtual方法并且声明该方法为sealed。则该方法不能被下一层中InvokingAgain类中所Override,但是,我们可以在一层中InvokingAgain类中隐藏Invoking中的sealed方法。贴上Demo更直观:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Demo
- {
- public class Invoking:Base
- {
- public sealed override double Operation(double x, double y)
- {
- return x*y;
- }
- }
- public class InvokingAgain : Invoking
- {
- public double Operation(double x, double y)
- {
- return x / y;
- }
- }
- }
在Main中的调用及结果,有图有真相 (:D):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Invoking invoking = new Invoking();
- Console.WriteLine(invoking.Operation(2,3));
- InvokingAgain invokingAgain = new InvokingAgain();
- Console.WriteLine(invokingAgain.Operation(2, 3));
- Console.ReadLine();
- }
- }
- }