Class, subclass, inheritance, virtual, abstract, override

Source: Internet
Author: User

My basic skills are not solid, so I have to try classes, subclasses, inheritance, virtual, abstract, and override today.

Rule 1: override a function with the same name as the parent class to use new, and override to override the function.
RelatedCode:
Class
{
Public void AAA ()
{
}
}

Class B:
{
Public void AAA ()
{
}
}
Error message: the keyword new is required on "B. AAA ()" because it hides the inherited member "A. AAA ()"
Overwrite, rewrite difference: when the parent item is forced, the effect is different: (a) bb. AAA ();

Rule 2: the parent item must be virtual, abstract, or override to be overwritten.
Related code:
Class
{
Public void AAA ()
{
}
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: "B. AAA ()": the inherited member "A. AAA ()" cannot be rewritten because it is not marked as virtual, abstract, or override

Rule 3: virtual objects must have entities
Related code:
Class
{
Public Virtual void AAA ();
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: "A. AAA ()" must declare the subject because it is not marked as abstract or extern

Rule 4: abstract methods can only be in abstract classes.
Class
{
Public abstract void AAA ();
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: "A. AAA ()" is abstract, but it is included in a non-abstract class "".

Rule 5: Abstract METHODS cannot declare subjects
Abstract Class
{
Public abstract void AAA ()
{
}
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: "A. AAA ()" cannot declare the subject because it is marked as abstract

Rule 6: the overwritten class must be a subclass.
Class
{
Public override void AAA ()
{
}
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: "A. AAA ()": no suitable method is found for rewriting.

Rule 7: Virtual and abstract cannot be used together.
Class
{
Public abstract virtual void AAA ()
{
}
}

Class B:
{
Public override void AAA ()
{
}
}
Error message: the abstract method "A. AAA ()" cannot be marked as virtual.
However, override and abstract can be used.
Class
{
Public Virtual void AAA ()
{
}
}

Abstract class B:
{
Public override abstract void AAA ();
}

Rule 8: Virtual and override cannot be used.
Class
{
Public Virtual void AAA ()
{
}
}

Class B:
{
Public override virtual void AAA ()
{
}
}
Error message: the member "B. AAA ()" marked as override cannot be marked as new or virtual

Rule 9: override cannot change the access modifier
Class
{
Public Virtual void AAA ()
{
}
}

Class B:
{
Protected override void AAA ()
{
}
}
Error message: "B. AAA ()": when the "public" inherited member "A. AAA ()" is rewritten, the access modifier cannot be changed.

Final standard writing
Class
{
Public Virtual void AAA ()
{
}
}

Class B:
{
Public override void AAA ()
{
}
}

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.