Introduction to interface abstract and virtual in c

  • 2020-05-17 06:14:39
  • OfStack

interface is used to declare interfaces
1. Provide only a set of method specifications, not the method body, as follows:


public interface IPerson
{
    void getName();// Does not contain the method body 
}

2. Method cannot be modified with public abstract, no field variables, no constructor.
3. Methods can contain parameters. Such as

  public interface IPerson
  {
    void getAge(string s);
  }

1 example (example 1) :

public interface IPerson
{ 
   IPerson();              // error 
   string name;            // error 
   public void getIDcard();// error 
   void getName();         //right
   void getAge(string s);  //right
}

Class that implements interface
1. To form 1 of an inherited class, e.g. public class Chinese:IPerson{}
2. The methods in interface must be implemented

Example 2, inheritance 1


public class Chinese:IPerson
{ 
   public Chinese(){}                  // Add structure 
   public void getName(){}          // implementation getName()
   public void getAge(string s){} // implementation getAge()
}

abstract declares abstract classes and abstract methods
1. The class in which the abstract method resides must be an abstract class
2. An abstract class cannot be instantiated directly; it must be implemented by a derived class.
3. An abstract method does not contain the body of the method and must be implemented by a derived class in an override manner, similar to the method in abstract

Such as


public abstract class Book
{
  public Book()
  {   
  }
  public abstract void getPrice();      // An abstract method without a body 
  public virtual void getName()   // Virtual method that can be overridden 
  {
      Console.WriteLine("this is a test:virtual getName()");
  }
  public virtual void getContent()   // Virtual method that can be overridden 
  {
      Console.WriteLine("this is a test:virtual getContent()");
  }
  public void getDate()                           //1 General method, if overridden in a derived class, must be used new The keyword 
  {
      Console.WriteLine("this is a test: void getDate()");
   }
}
public class JavaBook:Book
{
      public override void getPrice()   // To implement abstract methods, you must implement them 
      {
           Console.WriteLine("this is a test:JavaBook override abstract getPrice()");
      }
      public override void getName()   // Overriding the original method is not required 
      {
           Console.WriteLine("this is a test:JavaBook override virtual getName()");
      }
}

The tests are as follows:

public class test
{
   public test()
   {
    JavaBook jbook=new JavaBook();
         jbook.getPrice();      // Will call JavaBook In the getPrice()
         jbook.getName();       // Will call JavaBook In the getName()
         jbook.getContent();    // Will call Book In the getContent()
         jbook.getDate();       // Will call Book In the getDate()
    }
   public static void Main()
   {
       test t=new test();
   }
}

The virtual tag method is a virtual method
1. You can override this method in a derived class with override
2. Can be called by an object without overwriting
3. For methods without this tag (and no other tags), new should be used to hide the original method when rewriting

abstract and virtual: methods are rewritten using the override keyword
Both the methods in interface and the abstract methods require implementation

I found that many of my friends are not very clear about the difference between Abstract and Virtual functions in C#. Now I will use two pieces of code to let you know the difference between Abstract and Virtual functions
Development environment: VS.net 2005
How to use: override the code in program.cs with the following code, then run
Code 1: usage of Abstract


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
    public abstract class Book
    {
        // Abstract method, does not contain the body, the abstract method class must be abstract class, derived class must implement the method 
        public abstract void Introduce();
    }
    public interface iBook
    {
    }
    public class JavaBook : Book
    {
        // Implement abstract methods, must be implemented, must be added override The keyword 
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }
//            // Compile Error
//            public void Introduce()
//            {
//                Console.WriteLine("I'm Java");
//            }
    }
    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            javaBook.Introduce();      // Will call JavaBook In the Introduce()
            Book book = new JavaBook();
            book.Introduce();      // Will call JavaBook In the Introduce()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}

Code 2: the use of Virtual and the use of override

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
    public abstract class Book
    {
        public virtual void Introduce()
        {
            Console.WriteLine("I'm book");
        }
        public virtual void SayHi()
        {
            Console.WriteLine("Hi, I'm book");
        }
    }
    public class JavaBook : Book
    {
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }
        // Note that this method does not override Method of the parent class 
        public void SayHi()
        {
            Console.WriteLine("Hi, I'm Java");
        }
    }
    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            Book book = new JavaBook();
            javaBook.Introduce();       // Will call JavaBook In the Introduce()
            book.Introduce();       // Will call JavaBook In the Introduce()
            javaBook.SayHi();      // Will call JavaBook In the SayHi()
            book.SayHi();           // Will call Book In the SayHi()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}

Paragraph 3 code: usage of new

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
    public abstract class Book
    {
        public void Introduce()
        {
            Console.WriteLine("I'm book");
        }
        public void SayHi()
        {
            Console.WriteLine("Hi, I'm book");
        }
    }
    public class JavaBook : Book
    {
        //  without new , but C# The default behavior is added new
        public void Introduce()
        {
            Console.WriteLine("I'm Java");
        }
        //  Explicit plus new And do not add new The actual effect 1 Kind, just add new And then it will be eliminated compile warning
        public new void SayHi()
        {
            Console.WriteLine("Hi, I'm Java");
        }
    }
    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            Book book = new JavaBook();
            javaBook.Introduce();       // Will call JavaBook In the Introduce()
            javaBook.SayHi();           // Will call JavaBook In the SayHi()

            book.Introduce();       // Will call Book In the Introduce()
            book.SayHi();           // Will call Book In the SayHi()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}


Related articles: