易截截图软件、单文件、免安装、纯绿色、仅160KB

c# virtual虚函数的new、override实现

c#中基类(父类)中的某方法若想在派生类(子类)中被重写(override),必须将基类中的方法定义为virtual,即虚函数。
若派生类将方法修饰为new,即有意隐藏基类中的方法。
下面看一组代码:
public class Father
{
public void hand()
{
Console.WriteLine("Father.hand");
}
}
public class Son : Father
{
public void hand()
{
Console.WriteLine("Son.hand");
}
static void Main(string[] args)
{
Father son = new Son();
son.hand();
Console.ReadLine();
}
}

输出:Father.hand
警告:“Son.hand()”将隐藏继承的成员“Father.hand()”。若要使当前成员重写该实现,请添加关键字 override。否则,添加关键字 new。
public class Father
{
public void hand()
{
Console.WriteLine("Father.hand");
}
}
public class Son : Father
{
public override void hand()
{
Console.WriteLine("Son.hand");
}
static void Main(string[] args)
{
Father son = new Son();
son.hand();
Console.ReadLine();
}
}
 
编译错误:“Son.hand()”: 继承成员“Father.hand()”未被标记为 virtual、abstract 或 override,无法进行重写。
public class Father
{
public virtual void hand()
{
Console.WriteLine("Father.hand");
}
}
public class Son : Father
{
public override void hand()
{
Console.WriteLine("Son.hand");
}
static void Main(string[] args)
{
Father son = new Son();
son.hand();
Console.ReadLine();
}
}

输出:Son.hand
无警告。
public class Father
{
public void hand()
{
Console.WriteLine("Father.hand");
}
}
public class Son : Father
{
public new void hand()
{
Console.WriteLine("Son.hand");
}
static void Main(string[] args)
{
Father son = new Son();
son.hand();


相关文档:

c#实现下载代码收集

//TransmitFile实现下载
    protected void Button1_Click(object sender, EventArgs e)
    {
        /*
        微软为Response对象提供了一个新的方法TransmitFile来解决使用Respo ......

利用WM_COPYDATA在应用程序间传递数据(C#)

利用WM_COPYDATA在应用程序间传递数据很简单,开销也小
一、传递数据部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ThreeTorches
{
    public struct Copydatastru ......

C#面试笔试小贴士 2

对于多态,还必需提一个C#中的关键字:new。前面提到,对于virtual方法,JIT会确定实例的实际类型然后决定调用什么方法。但是如果派生类中new关键字修饰方法,则它向CLR澄清此派生类中的方法与基类中的方法毫无关系,以下代码最终调用是基类的introduce方法:
Code
class Program
{
    static vo ......

C#画线控件的开发应用实例解析

C#画线控件的应用实例介绍之前我们要明白在C#中没有画线的控件,这里写了一个,大家分享。共有两个控件分别是画横线和画竖线的,关于怎么画斜线有兴趣的可以做一个大家分享。
    C#画线控件之横线
using System;  
using System.Collections;  
using System.ComponentModel; ......

C#实现文件关联

和其他语言一样,C#实现文件关联同样需要直接操作注册表,即按规则分别设置文件扩展名,文档类型说明,友好名称,图标,调用方法等键值即可,网上随便查查就可以写出以下的代码。 using Microsoft.Win32; RegistryKey key = Registry.ClassesRoot.OpenSubKey(".jb");
if (key == null)
{
 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号