易截截图软件、单文件、免安装、纯绿色、仅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#代码与javaScript函数的相互调用


问:
1.如何在JavaScript访问C#函数?
2.如何在JavaScript访问C#变量?
3.如何在C#中访问JavaScript的已有变量?
4.如何在C#中访问JavaScript函数?
问题1答案如下:
javaScript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
2、在前台写一个js函数,内容为 ......

c# 类库

[SerializableAttribute]
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDual)]
public class Object
1 public virtual bool Equels (Object)
2 public static bool static Equels (Object,Object)
默认实现为对引用类型使用引用相等,对值类型使用二进制按位相等。引用相等性是指进 ......

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; ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号