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 ()
{
}
}