zoukankan      html  css  js  c++  java
  • 参数数组

    如果发生变化的不是参数的类型,而是参数的数量,重载就不那么适用了;

    使用参数数组,可以写出参数的数量可变的方法;

    public void Fun(string str, params  int[ ]  paramList);

    1,一个方法只能有一个参数数组,且位于最后;

    错误:public void Fun(params  int[ ]  paramList,string str,);

    2,参数数组不允许指定out或ref

    错误 :public void Fun(string str, out params  int[ ]  paramList);

    3,区分下面两种写法

    public void Fun(string str, int[ ]  paramList);

    public void Fun(string str, params  int[ ]  paramList);

    前者仅有两个参数,第二个参数是数组;后者参数不固定,可以有多个int参数;同时,这两个函数的签名是一样的,并不构成重载,会发生编译错误。

    4,非params方法优先于params方法调用

    已知一个类有如下三种方法:

    public void Fun(string str, int[ ]  paramList);

    public void Fun(string str, int a)

    public void Fun(string str, params  int[ ]  paramList);

    调用  Fun("哈哈哈",100);

    分析:

    (1)不可能调用第一个,因为它的第二个参数是数组类型,非整型

    (2)第二个和我第三个都能匹配住,但是会根据最佳匹配原则,调用第二个;

  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/maoshuyi/p/9914045.html
Copyright © 2011-2022 走看看