大家帮帮忙,小弟有问题要问? 我是C#初学者,刚学不久,感觉书都看的懂,只是在继承和多态有点模糊,不过看书勉强还是可以接受,但我现在不知道自己应该做怎样的题目,现在都是抄书上例子的代码,有曾试过做书后的练习,但感觉不到是用C#编的,觉得跟用C没什么区别,继承和多态根本在代码中体现不出来,小弟想问大家,接下去我怎么做才好,谢谢. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个委托的例子public class DelegateTest { public class MathOperations { public static double MultiplyByTwo(double value) { return 2*value; } public static double Square(double value) { return value*value; } } public delegate double DoubleOp(double value); public class MainEntryPoint { public static void Execute() { DoubleOp[] operations={new DoubleOp(MathOperations.MultiplyByTwo), new DoubleOp(MathOperations.Square)}; for(int i=0;i<operations.Length;i++) { MessageBox.Show(i.ToString()); System.Windows.Forms.MessageBox.Show(operations[i](111).ToString()); ProcessAndDisplayNumber(operations[i],2.0); ProcessAndDisplayNumber(operations[i],7.94); ProcessAndDisplayNumber(operations[i],1.414); } } /// <summary> /// 多点委托 /// </summary> public static void Execute2() { DoubleOp objTest = new DoubleOp(MathOperations.MultiplyByTwo); objTest += new DoubleOp(MathOperations.Square); //以上两行代码等价于如下代码: //DoubleOp A = new DoubleOp(MathOperations.MultiplyByTwo); //DoubleOp B = new DoubleOp(MathOperations.Square); //DoubleOp objTest = A + B; //委托可以相加,也可以相减 //多点委托的最终返回值是最后一个方法的返回值 MessageBox.Show(objTest(3).ToString());//9 } public static void ProcessAndDisplayNumber(DoubleOp action,double value) { double result=action(value); MessageBox.Show(result.ToString()); } } } 多态:public static string GetString(string strValue){ return strValue;}public static string GetString(string strValue1,string strValue2){ return strValue1;}public static string GetString(int intValue){ return intValue.ToString();}调用:string a=GetString("a");string b=GetString("a","b");string c=GetString(5);系统会根据你调用时的参数个数和参数类型自动识别该调用哪一个方法的 建议看一些微软提供的经典例子,可以在下面的网站中找到,www.asp.netwww.windowsforms.net 有问题多来csdn上问,或者看高手们的Blog,最重要的是自己要多实践。 可以去MSDN看看微软的例子。你也可以去download.chinaitlab.com中下载一些关于这方面的资料。 ◆理解事件委托◆ 【求助】XML表头的输出! 请问在C#中,用foreach如何将datatable C#边缘问题------指针与数组的相互转换 C#坐标原点在那儿,还有x轴,y轴的方向,向那边为正?? 郁闷,今天碰到了datatime问题 对于用c写的一个库,在c#里面怎么调用? 求救:.NET SDK快速入门示例数据库在xp下不能装? 有关窗体的简单问题? visual studio .net 安装好后不能用,怎么回事? 怎样读取一个ini文件中的所有section的值? 请教:datagrid中添加包含下拉框的模板列问题
{
public class MathOperations
{
public static double MultiplyByTwo(double value)
{
return 2*value;
}
public static double Square(double value)
{
return value*value;
} }
public delegate double DoubleOp(double value); public class MainEntryPoint
{
public static void Execute()
{
DoubleOp[] operations={new DoubleOp(MathOperations.MultiplyByTwo),
new DoubleOp(MathOperations.Square)}; for(int i=0;i<operations.Length;i++)
{
MessageBox.Show(i.ToString());
System.Windows.Forms.MessageBox.Show(operations[i](111).ToString());
ProcessAndDisplayNumber(operations[i],2.0);
ProcessAndDisplayNumber(operations[i],7.94);
ProcessAndDisplayNumber(operations[i],1.414);
}
} /// <summary>
/// 多点委托
/// </summary>
public static void Execute2()
{
DoubleOp objTest = new DoubleOp(MathOperations.MultiplyByTwo);
objTest += new DoubleOp(MathOperations.Square);
//以上两行代码等价于如下代码:
//DoubleOp A = new DoubleOp(MathOperations.MultiplyByTwo);
//DoubleOp B = new DoubleOp(MathOperations.Square);
//DoubleOp objTest = A + B;
//委托可以相加,也可以相减
//多点委托的最终返回值是最后一个方法的返回值
MessageBox.Show(objTest(3).ToString());//9 }
public static void ProcessAndDisplayNumber(DoubleOp action,double value)
{
double result=action(value);
MessageBox.Show(result.ToString());
}
} }
{
return strValue;
}public static string GetString(string strValue1,string strValue2)
{
return strValue1;
}public static string GetString(int intValue)
{
return intValue.ToString();
}调用:
string a=GetString("a");
string b=GetString("a","b");
string c=GetString(5);系统会根据你调用时的参数个数和参数类型自动识别该调用哪一个方法的
www.asp.net
www.windowsforms.net
你也可以去download.chinaitlab.com中下载一些关于这方面的资料。