我是C#初学者,刚学不久,感觉书都看的懂,只是在继承和多态有点模糊,不过看书勉强还是可以接受,但我现在不知道自己应该做怎样的题目,现在都是抄书上例子的代码,有曾试过做书后的练习,但感觉不到是用C#编的,觉得跟用C没什么区别,继承和多态根本在代码中体现不出来,小弟想问大家,接下去我怎么做才好,谢谢.

解决方案 »

  1.   

    给你个委托的例子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());
    }
    } }
      

  2.   

    多态: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);系统会根据你调用时的参数个数和参数类型自动识别该调用哪一个方法的
      

  3.   

    建议看一些微软提供的经典例子,可以在下面的网站中找到,
    www.asp.net
    www.windowsforms.net
      

  4.   

    有问题多来csdn上问,或者看高手们的Blog,最重要的是自己要多实践。
      

  5.   

    可以去MSDN看看微软的例子。
    你也可以去download.chinaitlab.com中下载一些关于这方面的资料。