virtual abstract override

virtual and abstract are used to modify the parent class, covered by the definition of the parent class, so that subclasses redefine.

They have one thing in common: If the method used to modify, add must be preceded by public, or else there will be a compilation error: virtual or abstract method method is not private. After all, plus virtual or abstract is to allow subclasses redefine, and private members are not accessible by subclasses.

But the big difference between them. (Virtual "virtual", abstract is "abstract").

(1) virtual method must be modified to achieve (even if it is just to add a pair of braces), and abstract method must not be modified to achieve. As for the virtual modified method if not implemented:

        public class Test1
        {
            public virtual void fun1();
        }

Error 2 "Test1.fun1 ()" must declare a body because it is not marked abstract, extern, or partial   

For abstract methods modified if there is to achieve:

        public abstract class Test2
        {
            public abstract void fun2() { }
        }

Error 1 "Test2.fun2 ()" can not declare a body because it is marked abstract  

(2) virtual subclasses can override, the abstract must be overridden by subclasses,

Copy the code
    class BaseTest1
    {
       public virtual void fun () {} // must implement 
    public virtual int func () {throw new NotImplementedException;} } class DeriveTest1:BaseTest1 { //public override void fun() { } }
Copy the code

Compile without error, if you override the virtual method of modification, it must be preceded by adding override (This tells the compiler that you want to override the virtual method), and must have realized, or compiler errors;

Copy the code
    abstract class BaseTest2
    {
        public abstract void fun();
    }
    class DeriveTest2 : BaseTest2
    {
        // public override void fun (); Error 1: not implemented
        // public void fun () {} Error 2: rewriting override not added
        // override void fun () {} Error 3: The virtual member or abstract members can not be private (as long as the member declares a virtual or abstract members in the parent class, even the inheritance should add this limit)
        public override void fun () {} // If override method; Error: "A.DeriveTest2" does not implement the inherited abstract member "A.BaseTest2.fun ()"    

    }
Copy the code

(3) If the class members are abstract modified, you must add the abstract before the class, because only an abstract class can have abstract methods.

(4) can not create an instance of abstract class can not be instantiated only be inherited, such as: BaseTest2 base2 = new BaseTest2 () ; Compile error: abstract classes or interfaces can not create an instance.
(5) C # if you want to override the method in the subclass must be added before the virtual parent class method, add the override before the subclass method, thus avoiding the programmer accidentally rewrite the parent class method in a subclass .

(. 6) must override abstract method, virtual must implement a method (method even if it is defined in the abstract class).

Copy the code
        abstract public class Test
        {
            // public virtual void Prinf (); Error: virtual method must have realized
            public virtual void Prinf () // virtual method abstract class may not overwritten; abstract method must be overridden.
            {
                Console.WriteLine("Abstract Printf...");
            }

        }
        public class Class1 : Test
        {
            /*
            When public override void Prinf () // derived class does not override the virtual method abstract class can still run, but calling Printf derived class object is called the parent class.
            {

                Console.WriteLine("Class One Override Printf...");
            }
             */
        }
Copy the code

 

Guess you like

Origin www.cnblogs.com/jshchg/p/11747004.html