MyClass equivalent in C#

前端 未结 3 1232
醉梦人生
醉梦人生 2021-02-18 22:33

In looking at this question, commenter @Jon Egerton mentioned that MyClass was a keyword in VB.Net. Having never used it, I went and found the document

3条回答
  •  独厮守ぢ
    2021-02-18 23:34

    In addition to answers saying it doesn't exist, so you have to make it non-virtual.

    Here's a smelly (read: don't do this!) work-around. But seriously, re-think your design.

    Basically move any method that must have the base one called into 'super'-base class, which sits above your existing base class. In your existing class call base.Method() to always call the non-overridden one.

    void Main()
    {
        DerivedClass Instance = new DerivedClass();
    
        Instance.MethodCaller();
    }
    
    
    class InternalBaseClass
    {
        public InternalBaseClass()
        {
    
        }
    
        public virtual void Method()
        {
            Console.WriteLine("BASE METHOD");
        }
    }
    
    class BaseClass : InternalBaseClass
    {
        public BaseClass()
        {
    
        }
    
        public void MethodCaller()
        {
            base.Method();
        }
    }
    
    class DerivedClass : BaseClass
    {
        public DerivedClass()
        {
    
        }
    
        public override void Method()
        {
            Console.WriteLine("DERIVED METHOD");
        }
    }
    

提交回复
热议问题