1. Basic concepts
1.1 override
The override keyword is used to override methods, properties, or indexers in the base class that have been declared virtual or abstract. Use the override keyword to provide a new implementation of the base class method. Its main features include:
- Polymorphism: By rewriting methods, derived classes can achieve specific behavior.
- Base class reference: Even if it is a base class reference, calling overwritten methods will execute the implementation of the derived class.
1.2 new
The new keyword is used to hide members (methods, properties, or events) in the base class. When the new keyword is used, members in the derived class hide members of the same name in the base class. Its features include:
- Polymorphism is not supported: base class references will still call the base class's methods, rather than the derived class's methods.
- Name Hiding: Members in derived classes obscures members of the same name in the base class, but the two still exist in different scopes.
2. Sample code
2.1. override keywords
The override keyword is used to override (overwrite) methods in a parent class in a subclass. Doing so allows subclasses to provide specific implementations to meet their individual needs for methods.
Example 1: Override the parent class method using override
using System; class Animal { public virtual void MakeSound() { ("Animal makes a sound"); } } class Dog : Animal { public override void MakeSound() { ("Dog barks"); } } class Program { static void Main(string[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); (); // Output: Animal makes a sound (); // Output: Dog barks } }
In this example, the Dog class inherits from the Animal class and overrides the MakeSound method. When () is called, it outputs "Dog barks" instead of "Animal makes a sound".
2.2. new keywords
The new keyword has two main uses in C#:
It is used to hide members (methods, properties, events, etc.) inherited from the base class.
It is used to provide implementations in an interface.
Example 2: Methods to hide inheritance using new
using System; class Animal { public virtual void MakeSound() { ("Animal makes a sound"); } } class Dog : Animal { new public void MakeSound() { ("Dog barks"); } } class Program { static void Main(string[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); (); // Output: Animal makes a sound (); // Output: Dog barks } }
In this example, although the Dog class overrides the MakeSound method, the MakeSound method of the base class Animal will not be hidden due to the use of the new keyword.
Example 3: Provide implementation in interface using new
using System; interface IMyInterface { void MyMethod(); } class MyClass : IMyInterface { public void MyMethod() { ("Interface method implementation"); } } class Program { static void Main(string[] args) { IMyInterface myClass = new MyClass(); (); // Output: Interface method implementation } }
In this example, the MyClass class implements the IMyInterface interface and uses new to provide the specific implementation of MyMethod.
3. Complete sample test
3.1 Definition of base and derived classes
using System; public class Animal { // A virtual method that can be rewritten in derived classes public virtual void Speak() { ("Animal speaks"); } // A normal method that can be hidden in derived classes public void Sleep() { ("Animal sleeps"); } } public class Dog : Animal { // Rewrite the virtual method of base class public override void Speak() { ("Dog barks"); } // Method of hiding the base class's same name using the new keyword public new void Sleep() { ("Dog sleeps"); } }
3.2 Test code
Next, we can write a test program to demonstrate the behavior of override and new.
class Program { static void Main(string[] args) { Animal myAnimal = new Animal(); (); // Output: Animal speaks (); // Output: Animal sleeps Animal myDog = new Dog(); (); // Output: Dog barks (polymorphism) (); // Output: Animal sleeps (base class method) Dog realDog = new Dog(); (); // Output: Dog barks (); // Output: Dog sleeps (newly defined method) (); } }
3.3 Operation results
Run the above program and the output result is as follows:
Animal speaks
Animal sleeps
Dog barks
Animal sleeps
Dog barks
Dog sleeps
analyze
Polymorphism: In the example, myDog is a reference to the Animal type, but it points to a Dog object. When calling the Speak method, the output is Dog barks, which is the manifestation of polymorphism. () calls the base class's Sleep method and outputs Animal sleeps because the Sleep method is not marked as virtual.
Hide behavior: () calls the new Sleep method defined in the Dog class and outputs Dog sleeps. This indicates that using the new keyword hides the same name method in the base class.
in conclusion
override and new are two keywords used in C# for version control and polymorphism. Using override, we can provide specific method implementations for subclasses, while new is used to hide members of base classes in the inheritance system or provide implementations for interfaces. These two keywords greatly enhance the flexibility and expressiveness of C# language.
The above is the detailed content of using Override and New keywords for version control in C#. For more information about Override and New version control in C#, please follow my other related articles!