Introduction to interface abstract and virtual in C #

Source: Internet
Author: User

Abstract and Virtual: method overrides use the Override keyword, both the method in interface and the abstract method require implementation

Interface used to declare an interface
1. Only some method specifications are provided, and no method body is provided. Such as:

Copy CodeThe code is as follows:
public interface IPerson
{
void GetName ();//Does not contain method body
}


2. Methods cannot be modified with public abstract, no field variables, no constructors.
3. The method can contain parameters. Such as

Copy CodeThe code is as follows:
public interface IPerson
{
void Getage (string s);
}


An example (Example 1):

Copy CodeThe code is as follows:
public interface IPerson
{
IPerson (); Error
String name; Error
public void Getidcard ();//Error

void GetName (); Right
void Getage (string s); Right
}


Classes that Implement Interface
1. Consistent with the format of the inheriting class, such as public class chinese:iperson{}
2. Each method in the interface must be implemented

Example 2, inheritance Example 1

Copy CodeThe code is as follows:
public class Chinese:iperson
{
Public Chinese () {}//Add construct
public void GetName () {}//implement GetName ()
public void Getage (string s) {}//implement Getage ()
}


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

Such as

Copy CodeThe code is as follows:
Public abstract class book
{
Public book ()
{
}

  public abstract void GetPrice ();     //abstract method with no body
  public virtual void GetName ()   //virtual method, can override
  {
      Console.WriteLine ("This is a test: Virtual GetName () ");
 }
  public virtual void getcontent ()   //virtual method, can override
  {
      Console.WriteLine ("This is a test:virtual getcontent ()");
 }
  public void getDate ()                            //General method, if overridden in a derived class, must use the New keyword
  {
      Console.WriteLine ("This is a test:void getDate ()");
  }
}

public class Javabook:book
{
public override void GetPrice ()//Implement abstract method, must implement
{
Console.WriteLine ("This is a Test:javabook override abstract GetPrice ()");
}
public override void GetName ()//Overwrite the original method, not required
{
Console.WriteLine ("This is a Test:javabook override virtual GetName ()");
}
}


The test is as follows:

Copy CodeThe code is as follows:
public class Test
{
Public test ()
{
Javabook jbook=new Javabook ();
Jbook.getprice (); Will call Javabook in GetPrice ()
Jbook.getname (); Will call Javabook in GetName ()
Jbook.getcontent (); Will call book in GetContent ()
Jbook.getdate (); Will call book in GETDATE ()

}
public static void Main ()
{

Test t=new test ();
}
}


Virtual methods for virtual tagging
1. You can override this method with override in a derived class
2. Do not overwrite or can be called by an object
3. Methods without this tag (and no other tags), overriding the original method with new

Abstract vs. Virtual: Use the Override keyword when overriding methods
Both the method in interface and the abstract method are required to implement

Find a lot of friends for C # in the abstract and virtual functions are not very clear, the following I pass two pieces of code to see what the difference between them ~ ~
Development environment: Vs.net 2005
How to use: Overwrite the code in Program.cs with the following code, then run to
First code: Usage of abstract

Copy CodeThe code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace ConsoleApplication3
{
Public abstract class book
{
Abstract method, without body, the class of abstract method must be abstract class, derived class must implement the method
public abstract void introduce ();
}
public interface IBook
{
}
public class Javabook:book
{
Implementing an abstract method must be implemented and the override keyword must be added
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 introduce ()
Book book = new Javabook ();
Book.      Introduce (); Will call Javabook in introduce ()
}
public static void Main ()
{
Test T = new Test ();
}
}
}


The second piece of code: Virtual usage and override usage

Copy CodeThe code is as follows:
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 the 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 introduce ()
Book.       Introduce (); Will call Javabook in introduce ()
Javabook.sayhi (); Will call Javabook in Sayhi ()
Book.           Sayhi (); Will call book in Sayhi ()
}
public static void Main ()
{
Test T = new Test ();
}
}
}


The third piece of code: the use of new

Copy CodeThe code is as follows:
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
{
No add new, but the C # default behavior is added to the new
public void Introduce ()
{
Console.WriteLine ("I ' m Java");
}
The explicit addition of new, like the actual effect without new, only adds new after the elimination of 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 introduce ()
Javabook.sayhi (); Will call Javabook in Sayhi ()

Book.       Introduce (); Will call book in introduce ()
Book.           Sayhi (); Will call book in Sayhi ()
}
public static void Main ()
{
Test T = new Test ();
}
}
}

Reprint: http://www.jb51.net/article/39944.htm

Introduction to interface abstract and virtual in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.