Overide和new和有什么不一样 [英] what is difference between overide and new

查看:125
本文介绍了Overide和new和有什么不一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用new关键字覆盖方法,则它将隐藏子类函数并调用父类函数

那么在覆盖方法中新"的需求是什么.最好不要重写子类中的方法.

我的意思是新"的用途是什么

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用使用System.Web.UI.WebControls;

命名空间 WebApplication1
{
    公共 部分  class  WebForm1:System.Web .UI.页面
    {
        受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
        {
            child c =  child();
            Response.Write(c.func1()+ "  + c.func2() );
        }
    }
    公共 父对象
    {
        公共 虚拟 字符串 func1()
        {
            返回 "  ;
        }
        公共 虚拟 字符串 func2()
        {
            返回 "  ;
        }

    }
    公共  child:父级
    {
        公共  字符串 func1()
        {
            返回 "  ;
        }
        公共 覆盖 字符串 func2()
        {
            返回 "  ;
        }
    }
} 

解决方案

override:为了能够override,在基类(该类中被覆盖)的方法(或属性)该类的来源)必须标记为virtual.重写方法后,将使用该方法是否强制实例化(也就是说,当将该类视为基类或派生类时)是此派生类或基类.一个很好的例子是查看ToString(). ToString()是在对象中定义的方法.您会看到重写ToString()将导致将使用任何类的ToString方法而不是object.ToString().

new:new 可以用于派生类中的任何方法(或属性).但是,如果将类强制转换为基类,则将执行基类中的方法,而不是派生类中的方法.仅当将类视为派生类(或从派生类派生的类)时,才会执行"new"方法.


重写可确保派生类方法始终为叫.使用new时,您将隐藏原始的基类方法,并声明一个新方法,如果将派生类强制转换为基类并调用该方法,则该新方法将不会被调用.使用override,您可以确保调用该方法将调用链中类的最新版本,并且当使用new时,将破坏此行为.
http://www.akadia.com/services/dotnet_polymorphism.html [
i mean what''s the use of "new"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            child c = new child();
            Response.Write(c.func1() + "<br>" + c.func2());
        }
    }
    public class parent
    {
        public virtual string func1()
        {
            return "parent Class function 1.";
        }
        public virtual string func2()
        {
            return "parent Class function 2.";
        }

    }
    public class child : parent
    {
        public new string func1()
        {
            return "child Class function 1.";
        }
        public override string func2()
        {
            return "child Class function 2.";
        }
    }
}

解决方案

override: To be able to override, the method (or property) to be overriden in the base class (the class that the class is derived from) must be marked as virtual. Once a method is overridden, that method will be used whether to instantiation is cast (which is to say that when the class is viewed as either the base class or derived class) as this derived class or the base class. A good example is to look at ToString(). ToString() is a method that is defined in the object. You can see that overriding the ToString() will result in the ToString method of any class will be used instead of the object.ToString().

new: new can be used on any method (or property) in a derived class. However, if the class is cast as the base class, the method in the base class will be executed, not the method in the derived class. Only if the class is viewed as the derived class (or a class derived from the derived class) will "new" method be executed.


Override makes sure the derived class method is always called. With new you hide the original base class method and declare a new one that won''t be called if the derived class is casted to the base class and the method is called. With override you ensure that calling the method will call the latest version of the class in the chain and when new is used you break this behaviour.
http://www.akadia.com/services/dotnet_polymorphism.html[^]

Good luck!


Override: Indicate you are overriding the base class method, meaning you are making base class method obselete and intentionally adding new functionality to it.

new: new indicate you are providing new definition to it, that recreating the other definition, it says the base and derived class method are different in context and the dervied has new definition to it


这篇关于Overide和new和有什么不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆