1. Comparison between override and new during method overwrite in C #
The override keyword in C # is used to overwrite the virtual method and abstract method;
In C #, The New Keyword is used to overwrite the "normal" function of the parent class in the derived class (that is, in the context of non-virtual functions and non-Abstract Functions)
Below isCodeExample: (for the usage of override, see the section comparing abstract and virtual below)
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Leleapplication4
{
Public Abstract Class Book
{
Public Void Introduce ()
{
Console. writeline ( " I'm book " );
}
Public Void Sayhi ()
{
Console. writeline ( " Hi, I'm book " );
}
}
Public Class Javatek: Book
{
// Explicitly adding new is the same as not adding new, but adding new will eliminate compile warning.
Public New Void Sayhi ()
{
Console. writeline ( " Hi, I'm Java " );
}
}
Public Class Test
{
Public Test ()
{
Javaink = New Javatek ();
Book = New Javatek ();
Javatek. Introduce (); // Will call introduce () in javatek ()
Javatek. sayhi (); // Will call sayhi () in javatek ()
Book. Introduce (); // Will call introduce () in the book ()
Book. sayhi (); // Will call sayhi () in the book ()
}
Public Static Void Main ()
{
Test t = New Test ();
}
}
}
2. Comparison between abstract and virtual in C #
Abstract methods are the same as those used in Java. At least I understand them now.
Virtual is a mechanism added in C # To implement Runtime polymorphism. As mentioned above, subclasses can overwrite the functions of the parent class (using override) to achieve polymorphism.
Let's take a note of the implementation of polymorphism in C #: The implementation of polymorphism in C # is divided into two types: compile-time polymorphism and Runtime polymorphism.
|-During compilation, polymorphism is implemented through the overloading of multiple methods in a class. during compilation, the system determines which method to call based on the passed parameters;
|-Runtime polymorphism is implemented through virtual functions (virtual functions) and abstract methods. The Derived classes use override virtual functions or abstract methods to achieve Runtime polymorphism.
The specific instance code is as follows:
Code 1: Abstract usage
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Leleapplication3
{
Public Abstract Class Book
{
// Abstract method, excluding the subject. The class of the abstract method must be an abstract class. The derived class must implement this method.
Public Abstract Void Introduce ();
}
Public Class Javatek: Book
{
// Abstract methods must be implemented. Note that! The override keyword must be added.
Public Override Void Introduce ()
{
Console. writeline ( " I'm Java " );
}
}
Public Class Test
{
Public Test ()
{
Javaink = New Javatek ();
Javatek. Introduce (); // Will call introduce () in javatek ()
Book = New Javatek ();
Book. Introduce (); // Will call introduce () in javatek ()
}
Public Static Void Main ()
{
Test t = New Test ();
}
}
}
Code 2: Virtual usage and override usage
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Leleapplication2
{
Public Abstract Class Book
{
Public Virtual Void Introduce ()
{
Console. writeline ( " I'm book " );
}
Public Virtual Void Sayhi ()
{
Console. writeline ( " Hi, I'm book " );
}
}
Public Class Javatek: Book
{
Public Override Void Introduce ()
{
Console. writeline ( " I'm Java " );
}
// Note that this method does not have the override parent class Method
Public Void Sayhi ()
{
Console. writeline ( " Hi, I'm Java " );
}
}
Public Class Test
{
Public Test ()
{
Javaink = New Javatek ();
Book = New Javatek ();
Javatek. Introduce (); // Will call introduce () in javatek ()
Book. Introduce (); // Will call introduce () in javatek ()
Javatek. sayhi (); // Will call sayhi () in javatek ()
Book. sayhi (); // Will call sayhi () in the book ()
}
Public Static Void Main ()
{
Test t = New Test ();
}
}
}