Comparison of override and new during method coverage in C #. Net learning notes 8-c #, comparison of abstract and virtual (including implementation of C # polymorphism)

Source: Internet
Author: User

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

 

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.