zoukankan      html  css  js  c++  java
  • C#基本语法学习(四)

    重载

      一个方法的名字和方法的参数列表称为方法的签名。C#根据方法签名来识别方法,如果两个方法签名不同那么他们就是两个不同的方法。

      重载可以是方法重载(包括构造函数重载)和运算符重载。方法重载指的是一组名字相同而参数列表不同的方法。但方法的返回值类型不同不能构成重载。

    1 public static int max(int a, int b)
    2 {
    3 
    4 }
    5 
    6 public static int max(int a, int b, int c)
    7 {
    8 
    9 }

      C#中除了方法可以重载,运算符(+、-、*)也可以被重载。运算符重载允许一个类或者结构支持某种运算符运算。当然也可以用方法实现与运算符重载相同的功能,但在某些情况下,使用运算符重载比使用方法更符合人们日常的方式和习惯。

      运算符重载语法如下:

      public static 返回类型 operator 运算符(参数列表)

      {

        //运算过程代码

      }

      在运算符重载定义语法中public、static和operator是固定的,必须在运算符重载定义中出现,public和static表示这个方法是公共的和静态的,operator表示这是一个运算符重载方法。参数列表个数与具体运算符对应。

      还有一种特殊的运算符重载,叫做类型转换运算符重载。语法如下:

      public static explicit|implicit operator 转换目的类型(参数)

      参数为待转换的数据,转换目的类型为想要得到的转换后的数据类型,explicit表示转换必须为显示转换,implicit表示转换可以是隐式转换。

      如下一维向量重载“+”和类型转换:

      1 namespace OperatorOverload
      2 {
      3        //一维向量 
      4     class Vector
      5     {
      6         private int _rank = 0;
      7         public int rank
      8         {
      9             get { return _rank; }
     10         }
     11 
     12         private double[] _values;
     13         public double[] values
     14         {
     15             get { return _values; }
     16         }
     17 
     18         public Vector(int n)
     19         {
     20             _rank = n;
     21             _values = new double[n];
     22         }
     23 
     24         public Vector(double[] doubleArray)
     25         {
     26             _rank = doubleArray.Length;
     27             _values = doubleArray;
     28         }
     29         
     30         //+重载
     31         public static Vector operator +(Vector a, Vector b)
     32         {
     33             if (a.rank == 0 || b.rank == 0)
     34                 return null;
     35 
     36             //向量秩不相等无法相加
     37             if (a.rank != b.rank)
     38                 return null;
     39 
     40             double[] sum = new double[a.rank];
     41             double[] valuesa = a.values;
     42             double[] valuesb = b.values;
     43 
     44             for (int i = 0; i < a.rank; i++)
     45             {
     46                 sum[i] = valuesa[i] + valuesb[i];
     47             }
     48 
     49             Vector result = new Vector(sum);
     50 
     51             return result;
     52         }
     53 
     54         //类型转换重载
     55         public static explicit operator double[](Vector v)
     56         {
     57             return v.values;
     58         }
     59 
     60         public static explicit operator Vector(double[] array)
     61         {
     62             return new Vector(array);
     63         }
     64 
     65         //重写ToString方法
     66         public override string ToString()
     67         {
     68             string result = "(";
     69             for (int i = 0; i < _rank - 1; i++)
     70             {
     71                 result += _values[i].ToString() + ", ";
     72             }
     73             result += _values[_rank - 1] + ")";
     74 
     75             return result;
     76         }
     77     }
     78 }
     79 
     80 namespace OperatorOverload
     81 {
     82     class Program
     83     {
     84         static void Main(string[] args)
     85         {
     86             Vector a, b, c;
     87 
     88             a = new Vector(new double[] { 11.1, 12.1, 13.1, 14.1, 15.1 });
     89             b = new Vector(new double[] {22.2, 32.2, 33.2, 34.2, 35.2});
     90             c = a + b;
     91             Console.WriteLine("Vector a=" + a.ToString());
     92             Console.WriteLine("Vector b=" + b.ToString());
     93             Console.WriteLine("Vector a+b=" + c.ToString());
     94 
     95             double[] array = new double[] { 1, 2, 3, 4, 5 };
     96             Vector d = (Vector)array;
     97 
     98             Console.WriteLine("array-->Vector: " + d.ToString());
     99 
    100             Console.ReadLine();
    101         }
    102     }
    103 }

     运行结果

    Vector a=(11.1, 12.1, 13.1, 14.1, 15.1)
    Vector b=(22.2, 32.2, 33.2, 34.2, 35.2)
    Vector a+b=(33.3, 44.3, 46.3, 48.3, 50.3)
    array-->Vector: (1, 2, 3, 4, 5)
  • 相关阅读:
    jmeter参数化
    安卓稳定性压测工具_monkey环境搭建(简易)
    安卓开发环境搭建
    linux环境下禅道搭建
    Elasticsearch 开源版、基础版、黄金版、铂金版功能差异
    防火墙与127.0.0.1
    基于x-pack的ES用户管理(认证)
    elasticsearch-keystore 命令简单解释
    Elasticsearch核心技术与实战-学习笔记
    程序设计随想
  • 原文地址:https://www.cnblogs.com/numbqq/p/5284575.html
Copyright © 2011-2022 走看看