C # in the abstract, virtual, interface keyword

A, Virtual (virtual method modified keywords)


     Method for modifying the virtual keyword in the base class. will use virtual two cases:

     Case 1: the virtual methods defined in the base class, but in the derived class virtual method is not overridden. Then the call to the derived class instance, the method using a virtual base class definition.

     Case 2: the virtual methods defined in the base class, and then override the use of the override method in a derived class. Then the call to the derived class instance, the virtual method is a method of using a derived rewritable.

Two, the Abstract (abstract method modified keywords)

     abstract in an abstract class keyword used method for modification, and no specific implementation. Implement the abstract method in a derived class must be used to achieve the override keyword, an abstract class is used inherited; can be seen as a virtual method is not achieved body; if the class contains an abstract method, then it must be defined as an abstract class class, regardless of whether other general method further comprises;

 

Three, interface (Interface modified keywords)

Difference: abstract class is an incomplete class is an abstract object, the interface is a code of conduct.

Example:

namespace Community.BLL.Demo 
{ 
    ///  <Summary> 
    /// interface definition, only the interface methods statute, does not provide a method body. Public abstract method can not be modified and the like, no field variables, no structural function.
    ///  </ Summary> 
    public  interface IPerson 
    { 
        void getName (); // no methods body 
        void getAge ( String S); // method may comprise parameters. 
    }
     ///  <Summary> 
    /// interface
     ///  </ Summary> 
    public  class Chinese: IPerson 
    { 
        public Chinese () {} // Add configuration 
        public  voidgetName () {} // implement getName () 
        public  void getAge ( String S) {} // implement getAge () 
    } 

    ///  <Summary> 
    /// abstract class defines
     /// abstract: declared abstract class, an abstract method
     /// 1. An abstract class is an abstract class where the method must
     /// 2. abstract class can not be instantiated directly, must be implemented by the derived class.
    /// 3. The method does not include abstract method body must override manner by derived classes implement this method, this method is similar to the interface with the
     ///  
    ///  </ Summary> 
    public  abstract  class BaseQuery 
    { 
        ///  < Summary> 
        /// constructor
         ///  </ Summary> 
        publicBaseQuery () {} 

        ///  <Summary> 
        /// abstract methods defined, without the subject, keywords abstract modification methods, and no specific implementation. Implement abstract methods must be implemented using the override keyword in the derived class.
        ///  </ Summary> 
        public  abstract  void the Run (QueryRecord queryRecord); 

        ///  <Summary> 
        /// general method, when overridden in a derived class, must use the new keyword
         ///  </ Summary> 
        public  void StartQuery (queryRecord queryRecord, Dynamic queryParams) 
        { 
            var objA = new new {queryParams.Name, queryParams.IdentifyNumber};
             var objB = new new {objA}; 
        } 
        /// <Summary> 
        /// virtual method, can cover, not necessarily
         ///  </ Summary> 
        public  Virtual  void GetName () 
        { 
        } 
    } 

    ///  <Summary> 
    /// derived class definitions 001
     ///  </ Summary > 
    public  class Query001: BaseQuery 
    { 
        ///  <Summary> 
        /// derived class override keyword used to implement the abstract method in a base class
         ///  </ Summary> 
        public  override  void the Run (QueryRecord queryRecord) 
        { 
            var queryParams = newQueryRecord.Name {, queryRecord.IdentifyNumber};
             // call the method in the base class 
            Base .StartQuery (queryRecord, queryParams);
             // do not override the virtual method. Then the call to the derived class instance, the method using a virtual base class definition. 
            Base .getName (); 
        } 
        ///  <Summary> 
        /// using the override override this method. Then the call to the derived class instance, the virtual method is a method of using a derived rewritable.
        ///  </ Summary> 
        public  the override  void GetName () {} 

        ///  <Summary> 
        /// general method, when overridden in a derived class, must use the new keyword
         ///  </ Summary> 
        new  public  void StartQuery (queryRecord queryRecord, Dynamic queryParams)
        {
        } 
    } 

    ///  <Summary> 
    /// derived class definitions 002
     ///  </ Summary> 
    public  class Query002: BaseQuery 
    { 
        ///  <Summary> 
        /// derived class to use the override keyword in the base class abstract method
         ///  </ Summary> 
        public  the override  void the Run (queryRecord queryRecord) 
        { 
            var queryParams = new new {queryRecord.Name, queryRecord.IdentifyNumber, Mobile = " 138 888 888 " , a CreateDate = the DateTime.Now};
             // call the base class the method 
            base.StartQuery (queryRecord, queryParams); 
        } 
    } 
    ///  <Summary> 
    /// Query the recording
     ///  </ Summary> 
    public  class QueryRecord 
    { 
        public  String the Name { GET ; SET ;}
         public  String IdentifyNumber { GET ; SET ;}
         public  String QueryType { GET ; SET ;} 
    } 

    ///  <Summary> 
    /// abstract method called
     ///  </ Summary> 
    public class QueryBLL
    {
        /// <summary>
        /// 动态调用不同的类(Query001、Query002)
        /// </summary>
        public void RunQuery()
        {
            List<QueryRecord> list = new List<QueryRecord>();
            list.Add(new QueryRecord() { Name = "张三", IdentifyNumber = "110111000000000001", QueryType = "Query001" });
            list.Add(new QueryRecord() { Name = ""John Doe, IdentifyNumber = "110111000000000002", QueryType = "Query002" });

            foreach (QueryRecord queryRecord in list)
            {
                var queryType = queryRecord.QueryType;
                string assemblyName = this.GetType().Assembly.GetName().Name;
                var path = string.Format("{0}.{1},{2}", this.GetType().Namespace, queryType, assemblyName);
                BaseQuery obj = (BaseQuery)Activator.CreateInstance(Type.GetType(path));
                obj.Run(queryRecord);
            }
        }
    }

}
View Code

unit test

        #region call abstract class test 
        [TestMethod] 
        public  void CallAbstractClassTest () 
        { 
            QueryBLL BLL = new new QueryBLL (); 
            bll.RunQuery (); 
        } 
        #endregion

 

Published 259 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/hofmann/article/details/104833007