zoukankan      html  css  js  c++  java
  • 浅析c#中new、override、virtual关键字的区别

    最近简单的复习了在c#中涉及到继承这个特性时,所需要用到的关键字,其中有一些关键点,特地整理出来,方便大家查阅。在一次笔试和面试中也提到这个问题:
    一、在c#中,new这个关键字使用非常频繁,主要有3个功能:
    (1)作为运算符用来创建一个对象和调用构造函数;
    (2)作为修饰符;
    (3)用于在范型声明中约束可能用作类型参数的参数的类型
    在本文中,只具体介绍new作为修饰符的作用。
    用作修饰符时,new关键字可以在派生类中隐藏基类的方法。
    也就是说派生类调用的方法是new关键字新定义出来的方法,而不是基类的方法。
    在不使用new关键字来隐藏基类方法也是可以的,编译器会出现一个警告,提示如果有意隐藏基类的方法,请使用new关键字修饰。

    这里有个注意事项就是new和override这两个关键字是互斥的。不可以同时使用。

    二、override关键字主要是提供派生类对基类方法的新实现,重写的基类方法必须和override的方法具有相同的签名。此关键字不可以用于重写非虚方法和静态方法,与其配套使用的关键字是virtual,abstract,override。与此同时,override方法还不可以修改virtual方法的可访问性,override方法和virtual方法必须具有相同的访问修饰符。不能使用修饰符new,static,virtual或者abstract来修改override方法。

    三、virtual关键字允许在派生类中重写这些对象,默认情况下,方法是非虚拟的,不可以重写非虚方法。virtual关键字不可以与static,abstract,private,override一起使用。virtual关键字和override紧密不可分的,如果要实现virtual方法就必须使用override或new关键字(new和override产生的机理不同)。
    总结:new关键字主要用来区别派生类和基类同名方法的选择问题,通过隐藏基类方法,达到使编译器调用正确的方法的目的。override主要用来对基类的方法和虚方法进行重写。

    IHandler.cs:
    ================================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    public interface IHandler
    {
    string Name { get; set; }
    }
    }
    
    
    Handler.cs
    ==================================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    public abstract class Handler:IHandler
    {
    public abstract string Name { get; set; }
    
    public Handler() 
    {
    
    }
    
    public virtual void Perform()
    {
    Console.WriteLine(Name);
    }
    }
    }
    
    
    SpecifiedFunction.cs
    ======================================
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    public class SpecifiedFunction:Handler,IHandler
    {
    private string _name;
    
    public override string Name 
    {
    get { return this._name; }
    set { this._name = value; }
    }
    
    public SpecifiedFunction(string strInput)
    {
    this._name = strInput;
    }
    
    public override void Perform() 
    {
    this._name = string.Format("Hello,Name:{0}!", Name);
    base.Perform();
    //特别注意:
    /*
    在构造函数里面实现不了这种效果的:先进行子类操作然后再进行基类操作。
    在构造函数里面:
    * 1,方法名后面的调用会在.ctor操作之前执行:;
    * 2,在.ctor里面不能使用base关键字;
    * 
    * 但是在其他方法里面,我们可以base.Method(),非常好啊!呵呵!
    */
    }
    }
    }
    Program.cs
    =======================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Start!");
    SpecifiedFunction specifiedFunction = new SpecifiedFunction("Bob");
    specifiedFunction.Perform();
    Console.Read();
    }
    }
    }
    
    
    结果显示:
    Start!
    Hello,Name:Bob!
    
    达到了预期效果,非常好!

     

  • 相关阅读:
    正则表达式-元字符
    利用shell脚本实现nginx 的logs日志分割
    Linux下 nfs部署
    Linux下 niginx部署
    linux 硬盘分区与格式化挂载 (二)
    linux 硬盘分区与格式化挂载
    linux 系统监控
    [LeetCode] Best Time to Buy and Sell Stock
    [LeetCode] Convert Sorted Array to Binary Search Tree
    [LeetCode] Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/2747258.html
Copyright © 2011-2022 走看看