C #: Differences and Similarities between abstract virual

Source: Internet
Author: User

 

1:

Abstract METHODS must not be implemented and must be implemented by an inherited class (if this class is not an abstract class.

The class that declares the abstract method must be the abstract class, and the abstract class can have other non-abstract methods.

 

2:

The virual method must be implemented. do not inherit class implementation.

Class that declares virtual methods, not abstract class

3:

Abstract methods are only pure versions of virtual methods, and abstract methods also have dynamic mechanisms of virtual Methods (that is, the runtime calls corresponding methods based on the specific types referenced by objects, it can also be said that the method with the deepest hierarchy is called in the current reference ).

4.

Override two methods, which must be declared with override in the derived class. If override is not used

 

For the abstract method:

 

The compiler reports an error because the abstract class method is not implemented.

 

For the virtual method:

 

It is equivalent to overwriting the virtual method. A parameter list and return value are used, and the function name is the same. This method is no longer a virtual method, but a common function. This function will no longer be searched during the virtual function search.

 

In this case, the compiler will give a warning. because you have replaced the virtual method with almost identical function prototype methods. if you do need to do this, add the new Keyword before the method to show that you want to overwrite the virtual method.

 

The compiler no longer gives warnings.

 

 

In summary, in the abstract method, the virual method is consistent with the pure virtual function in C ++.

 

C ++

1.

Pure virtual functions must not be implemented

Classes that declare pure virtual functions must be abstract base classes (ABC) (automatically becomes without additional declarations)

2.

Instead of pure virtual functions, it must be implemented. Otherwise, it becomes a pure virtual function (unless the method is declared, it is not defined, and it is not pointed out after the declaration that this is a pure virtual function, that is, "= 0". In this case, any statement that calls this method will cause a link error, which is equivalent to a useless function)

Classes with virtual functions do not need to be abstract base classes.

3.

Both pure and virtual functions have the same dynamic mechanism.

 

Therefore, the conclusion is:

 

Abstract method in C #. The virual method is consistent with pure virtual functions in C ++.

 

 

Test code:

 

Abstract public class animal

{

Public abstract string get ();

Public virtual string eat () {return "animal ";}

}

Abstract public class waterAnimal: animal

{

// Public override string get ()

//{

// Return ToString ();

//}

Public override string eat () {return "wateranimal ";}

}

Public class fish: waterAnimal

{

Public override string get ()

{

Return ToString ();

}

New public string eat () {return "fish ";}

}

 

 

Public class landAnimal: animal

{

Public override string get ()

{

Return this. ToString ();

}

Public string get (int)

{

Return "notOverride ";

}

}

 

 

Public class cat: landAnimal

{

Public override string get ()

{

Return this. ToString ();

}

}

Public class dog: landAnimal

{

Public override string get ()

{

Return this. ToString ();

}

}

 

 

Class Program

{

Static void Main (string [] args)

{

Animal a = new fish ();

Console. WriteLine (a. eat ());

// WaterAnimal w = new waterAnimal ();

// Console. WriteLine (w. eat ());

 

A = new cat ();

LandAnimal l = new cat ();

Console. WriteLine (a. get ());

Console. WriteLine (l. get ());

 

A = new dog ();

 

Try

{

Cat c = a as cat;

C = (cat);

} Catch (Exception e)

{

Console. WriteLine (e. Message );

}

}

}

 

 

Correct the mistakes.

 

From the column zbwzll2

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.