Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between C# using new and override to implement abstract class rewriting

Detailed explanation of the difference between C# using new and override to implement abstract class rewriting

黄舟
黄舟Original
2017-03-06 11:12:051633browse

This article mainly introduces the detailed explanation of the difference between rewriting abstract classes using new and override in C#. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to have a look.

1. Abstract implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Virtualdemo
{
  class Program
  {
    static void Main(string[] args)
    {//BClass A = new BClass(); 抽象类无法被实例
      Class1 c = new Class1();
      BClass c2 = c;
      c2.MethodA();
      c.MethodA();
      c2.MethodB();
      c.MethodB();
      c2.MethodC();
      c.MethodC();
      Console.ReadKey();
    }
  }
  abstract class BClass  //抽象类无法被实例
  {
    public virtual void MethodA() { Console.WriteLine("BClassA"); }
    public virtual void MethodB() { Console.WriteLine("BClassB"); }
    public virtual void MethodC() { Console.WriteLine("BClassC"); }
  }
  class Class1 : BClass
  {
    public void MethodA() { Console.WriteLine("MethodA"); }//如果一个虚函数 在子类中没有通过override关键字,那么这个方法就没有被重写,而是被隐藏了
    public override void MethodB() { Console.WriteLine("MethodB"); }
    public override void MethodC() { base.MethodC(); }
  }
}

The result from the above figure is:

To sum up: the virtual method of an abstract class needs to be rewritten. Then the question arises. There are two ways to rewrite. What is the difference between new and override?

2. The difference between new and override rewriting:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Virtualdemo
{
  class Program
  {
    static void Main(string[] args)
    {
      Teacher t = new Teacher();
      Student stu = t;
      stu.Exam();
      stu.Say();
      t.Say();
      Console.ReadKey();
    }
  }
  public abstract class Student
  {
    public virtual void Exam()
    {
      Console.WriteLine("学生都要考试");
    }
    public void Say()
    {
      Console.WriteLine("我是学生");
    }
  }
  public class Teacher : Student
  {
    public override void Exam()
    {
      Console.WriteLine("老师不用考试");
    }
    //public override void Say() //报错,提示没有virtual,无法被重写
    //{
    //  Console.WriteLine("我是老师");
    //}
    public new void Say()
    {
      Console.WriteLine("我是老师");
    }
  }
}

The result is displayed as:

To summarize:

1. If the base class does not define a virtual method, then the subclass can only use new, You cannot use override

2. Whether the base class defines a virtual method, the subclass can use new

3. Using new in a subclass is not called rewriting, but a new creation of the subclass. A method, but the newly created method happens to have the same name as the method in the base class

The above is a detailed explanation of the difference between C# using new and override to implement abstract class rewriting. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn