C#_类的方法成员

类的方法成员:

             1.种类:

1.实例方法

void a ()
{}

2.静态方法

static void a ()
{}

             2.格式:

修饰符+返回结果类型+方法名称+(参数表){方法体}

             3.方法的参数:

1.值参数:

1.规定:
  1. 传递参数的值(使用值的时候要对其地址进行操作,所以要转成值类型在前面加ref
  2. 方法里面可以是值类型也可以是引用类型
  3. 使用时要提前初始化

2.值类型值参数:
//值类型值参数
static void Main(string[] args)
        {
            1
            int c = 4, d = 7;
            Swap1(c, d);
            Console.Write("c={0}, d={1}", c, d);
        }
static void Swap1(int a, int b)
        {
            int t = a;
            a = b;
            b = t;
        }
3.引用类型值参数:
//引用类型值参数
static void Main(string[] args)
        {
            int[] e = new int[4] { 1, 2, 3, 4 };
            int[] f = new int[4] { 5, 6, 7, 8 };
            Swap2(e, f);
        }
 static void Swap2(int[] a, int[] b)
        {
            int[] t = a;
            a = b;
            b = t;
            foreach (int i in a)
                Console.Write(i.ToString() + " ");
            foreach (int i in b)
                Console.Write(i.ToString() + " ");
        }

2.引用参数:

1.规定:

ref传递参数的地址(要改动其值就要改动他的地址下的参数array[0]);使用前要对其初始化;可以改值

2.值类型引用参数:

//值类型引用参数
static void Main(string[] args)
        {
            int c = 4, d = 7;
            Swap3(ref c, ref d);
            Console.Write("c={0}, d={1}", c, d);
        }
static void Swap3(ref int g, ref int h)
        {
            int t = g;
            g = h;
            h = t;
        }

3.引用类型引用参数:

//引用类型引用参数
static void Main(string[] args)
        {
            int[] p = new int[2] { 1, 2 };
            int[] q = new int[2] { 3, 4 };
            Swap4(ref p, ref q);
        }
static void Swap4(ref int[] m, ref int[] n)
        {
            int[] temp;
            temp = n;
            n = m;
            m = temp;
            foreach (int i in m)
                Console.Write(i.ToString() + " ");
            foreach (int i in n)
                Console.Write(i.ToString() + " ");
        }


3.输出参数:

1.规定:

out参数的内容可以修改并且可以返回参数的值;调用变量不需要提前初始化;可以改值

2.值类型输出参数

//值类型输出参数
static void Main(string[] args)
        {
            int small, large;
            Console.Write("answer={0}, small={1}, large={2}", Out1(out small, out large), small, large);
        }
 static int Out1(out int small, out int large)
        {
            small = 9;
            large = 24;
            return 15;
        }

3.引用类型输出参数:

//引用类型输出参数
static void Main(string[] args)
        {
            int[] one, two;
            Console.Write("total={0}", Out2(out one, out two));
        }
 static int[] Out2(out int[] one, out int[] two)
        {
            int[] t = new int[2] { 5, 6 };
            one = new int[2] { 1, 2 };
            two = new int[2] { 33, 44 };
            foreach (int index in one)
                Console.WriteLine(index);
            foreach (int jndex in two)
                Console.WriteLine(jndex);
            return t;
        }

4.数组参数:

1.规定:

 params类型[] 名称  一个方法只能有一个数组参数,并且要放在所有参数最后

2.数组参数1:

//数组参数1
static void Main(string[] args)
        {
            Program pro = new Program();
            int[] c = new int[3] { 5, 7, 3 };
            Console.WriteLine("Sum1:{0}", pro.Sum(c));
        }
int Sum(params int[] x)
        {
            int sum = 0;
            for (int i = 0; i < x.Length; ++i)
                sum += x[i];

            return sum;
        }

2.数组参数2:

//数组参数2 
static void Main(string[] args)
        {
            Program pro = new Program();
            Console.WriteLine("Sum2:{0}", pro.Sum(13, 45, 8, 24132, 1, 4, 8));
        }
string Sum(params string[] x)
        {
            string sum = "";
            for (int i = 0; i < x.Length; ++i)
                sum += x[i];

            return sum;
        }

             4.形参、值参、引用参数:

1.形参:存地址的变量

           2.值参:无法在方法里改其值

           3.引用参数:只改变变量,就不会对其堆的数值进行改变

     5.数组类型的参数和数组参数:

1.数组类型的参数:其为数组在内存上确实的存在

           2.数组参数:数组参数必须放在参数的最后面,这样前面先取值,剩下的给数组

     6.外部静态方法和默认值:

1.外部静态方法:static extern

           2.值类型默认值:0;引用类默认值为:null

     7.泛型方法:

1.格式:

修饰符 返回结果类型 方法名称<T>(T 参数名){}

          2.规定:

其只能用“=”,不能用其他的运算符,主要用于运行游戏对象数量的修改

3.应用:

//泛型方法应用
static void Main(string[] args)
        {
            Program pro = new Program();
            Console.WriteLine("{0}", pro._Minus(35, 70));
            Console.WriteLine("{0}", pro._Minus(0.25f, 0.75f));
            Console.WriteLine("{0}", pro._Minus(0.456, 0.4874));
        }
double _Minus<T1>(T1 x, T1 y)
        {
            return Convert.ToDouble(x) * Convert.ToDouble(y);
        }                        
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值