Override and new override parent class virtual method (new and virtual coexist)

Source: Internet
Author: User

1. abstract class methods and virtual methods can be rewritten, but we say professionally: we have "implemented" the abstract method, and we have "rewritten" the virtual method.

2. abstract classes do not have instances, so abstract methods cannot be directly called (no method body) and can be implemented. abstract classes can write Virtual Methods and rewrite them.

3. The virtual method can no longer exist in the abstract class or the abstract class. The virtual method exists in the non-abstract class and can be called by the instance.

4. Override and overload are two concepts. Rewriting and overloading are literally different.

5. When override is used to override the parent class method in a derived class, I call this method directly or call the method through the method.

6. New Note: The condition exists when the method name of the derived class is the same as that of the parent class, otherwise an error will be reported (you use a new method that does not exist in the parent class, that is meaningless)

7. new is used. New is the new meaning. As long as a method independent from the parent class is generated and the parent class method is hidden, this method must have the same name as the parent class method, this method of the derived class is the same as that of the parent class. Although it is literally the same, it has no relationship with each other and is independent of each other. However, you can call the methods of the parent class.

 

 Class Test
{
Static Void Main ()
{
Father =New Father ();
Father. speaking1 (); // The father Method
Father. speaking2 (); // The father Method

Child child = New Child ();
Child. speaking1 (); // The child Method
Child. speaking2 ();// The child Method
}
}
Class Father
{
Public Virtual Void Speaking1 ()
{
Console. writeline ( " The father Method " );
}

Public Virtual Void Speaking2 ()
{
Speaking1 ();
}
}

Class Child: Father
{
Public Override Void Speaking1 ()
{
Console. writeline ( " The child Method " );
}
}

Override:

1. When you rewrite the ELE. Me speaking1 method, whether it is a direct call or a method call, it is a method to call the rewrite method.

New:

Father =NewFather ();
Father. speaking1 ();//The father Method
Father. speaking2 ();//The father Method

Child child =NewChild ();
Child. speaking1 ();//The child Method
Child. speaking2 ();//The father method is different.
 
 

New, The speaking1 method is independent. When you call the speaking2 method, it calls the father method.

 
 

 

 
 

NOTE: If new is used, but this method does not have a parent class, the system will prompt "inherited members are not hidden and new is not required"

 
 

New: although new hides the method of the parent class, it can call the method of the parent class.

ClassTest
{
Static VoidMain ()
{

Child child = New Child ();
Child. speaking1 (); // The father method the child Method
Child. speaking2 (); // The father Method
}
}
Class Father
{
Public Virtual Void Speaking1 ()
{
Console. writeline ( " The father Method " );
}

Public Virtual VoidSpeaking2 ()
{
Speaking1 ();
}
}

ClassChild: Father
{
Public New VoidSpeaking1 ()
{
Base. Speaking1 ();
Console. writeline ("The child Method");
}
}

About the co-existence of new and virtual:

  Class Test
{
Static Void Main ()
{

Third = New Third ();
Third. speaking2 (); // The father Method
Third. speaking1 (); // The third method


}
}
Class Father
{
Public Virtual Void Speaking1 ()
{
Console. writeline ( " The father Method " );
}

Public Virtual Void Speaking2 ()
{
Speaking1 ();
}
}

Class Child: Father
{
Public New Virtual Void Speaking1 ()
{
Base . Speaking1 ();
Console. writeline ( " The child Method " );
}
}

Class Third: Child
{
Public Override Void Speaking1 ()
{
Console. writeline ( " The third method " );
}
}

Analysis:

 

1. Because the third class overrides the speaking1 method of the Child class (inherited from the Child method rather than the father method): (similar to the Child method, it is an independent method of the base class speaking1)

Note: The Virtual of the base class cannot be extended to the new method. (Because they are independent)

Third. speaking2 (); The speaking1 in the method of calling the base class's speaking2 is the method of the base class.

 
 

 

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.