C#. NET inside abstract class, interface, virtual method

Source: Internet
Author: User

1 abstract class

(1) Abstract methods are declared only, not implementations, and can be seen as virtual methods without implementing the body.
(2) Abstract classes cannot be instantiated
(3) Abstract classes can, but do not have to have abstract attributes and abstract methods, but once you have an abstract method, you must declare this class as an abstract class
(4) The specific derived class must override the abstract method of the base class
(5) An abstract derived class can override an abstract method of a base class, or it can be overridden. If they are not overwritten, their specific derived classes must overwrite them. Such as:

usingSystem; Public Abstract classA//Abstract Class A{     Private intnum=0;  Public intNum//abstract class contains properties    {         Get         {             returnnum; }         Set{num=value; }             }     Public Virtual intGetnum ()//abstract class contains virtual methods    {         returnnum; }     Public voidSetnum (intN// //abstract classes contain common methods    {          This. num =N; }     Public Abstract voidE ();//abstract method in Class A E    } Public Abstract classB:a//because Class B inherits the abstract method E from Class A, Class B also becomes an abstract class{     } Public classc:b { Public Override voidE ()//overrides an abstract method inherited from Class A. If Class B itself also defines an abstract method, you must also override the    {         //throw new Exception ("The method or operation is not implemented.");    } } Public classTest {Static voidMain () {c C=NewC ();     C.E (); } }



Second, the interface

(1) interface cannot be instantiated
(2) The interface can only contain method declarations
(3) The members of an interface include methods, properties, indexers, events
(4) The interface cannot contain constants, fields (domains), constructors, destructors, static members. Such as:

 Public Delegate voidEventHandler (Objectsender, Event e); Public InterfaceITest {//int x = 0;    intA {Get; Set; }    voidTest (); EventEventHandler Event; int  This[intIndex] {         Get; Set; } }



(5) All members in the interface default to public, so the interface cannot have the private modifier
(6) Derived classes must implement all members of an interface
(7) A class can directly implement a plurality of interfaces, the interface is separated by commas
(8) An interface can have multiple parent interfaces, and the class implementing the interface must implement all the members of all parent interfaces

Iii. abstract classes and Interfaces
Same point:
(1) Can be inherited
(2) can not be instantiated
(3) can contain method declarations
(4) A derived class must implement a method that is not implemented
  difference: 
(1) Abstract base classes can define fields, properties, method implementations. An interface can only define properties, indexers, events, and method declarations, and cannot contain fields. The
(2) abstract class is an incomplete class that needs further refinement, while an interface is a behavior specification. Microsoft's custom interface is always behind the able field, proving that it is stating a class of "I can do ... The
(3) interface can be multiple implementations, the abstract class can only be inherited by a single
(4) Abstract class is more defined in a series of tightly related classes, and the interface is mostly loose but the implementation of a function of the class
(5) abstract class is a series of related objects abstracted from the concept, Thus reflects the internal commonality of things; an interface is a functional contract defined to satisfy an external invocation, so it reflects the external nature of the thing. The
(6) interface basically does not have any specific characteristics of inheritance, it only promises the method that can be called
(7) interface can be used to support callbacks, and the following Does not have this feature
(8) The concrete method of the abstract class implementation is virtual, but the interface method in the class that implements the interface defaults to non-virtual, but you can also declare it as virtual
(9) If an abstract class implements an interface, you can map the method in the interface to an abstract class as an abstract method without having to implement it. Implementing an interface in a subclass of an abstract class

 Four. Virtual method  

1. A method that contains the virtual modifier is called a dummy method virtual method

2. Subclasses can optionally override this method, or they may not override it; abstract ) method, the subclass must be overridden.

  3. Virtual methods can have entities, such as:
public virtual void Vt ()
{
Console.WriteLine ("A");
}
Therefore, virtual methods can be called directly, whereas abstract methods cannot declare method entities, public abstract void Ab (), and cannot be called directly.

C#. NET inside abstract class, interface, virtual method

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.