zoukankan      html  css  js  c++  java
  • c#方法学习部分

    1参数数组的使用 关键字params 类似于可变长数组和可选参数使用格式如下

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace ConsoleApplication4

    {

        class methodStample

        {

            static void Main(string[] args)

            {

                methodStample ms = new methodStample();

                ms.DoSomething("a");

                ms.DoSomething("b",1);

                ms.DoSomething("c",1,2);

                int[] array = { 1, 2, 3, 4 };

                ms.DoSomething("d",array);

               

               

            }

            public void DoSomething(string str, params int[] values) {

                if (values != null && values.Length > 0)

                {

                    for (int i = 0; i < values.Length; i++)

                    {

                        Console.WriteLine("{0},{1}", str, values[i]);

                    }

     

                }

                else {

                    Console.WriteLine(str);

                }

            }

           

           

            

        }

     

    }

     

    2可选参数,命名参数,方法重载,输出参数,值传递参数,引用传递参数

    3栈帧是编译器用来实现方法调用的一种数据结构是方法调用时的表示方法的载体,函数调用的所有调用信息保存在对应的栈帧中,其中栈帧保存的信息主要有:

    方法参数

    方法中的局部变量

    方法执行完后 的返回地址

    获取文件中包含的执行代码的行号和列号

    获取执行的代码 的文件名

    。。。。。。

    4递归

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace ConsoleApplication4

    {

        class methodStample

        {

            static void Main(string[] args)

            {

                methodStample ms = new methodStample();

                int result = ms.factorial(5);

                Console.WriteLine(result);

               

               

               

            }

     

            public int factorial(int n) {

                if (n == 1)

                {

                    return 1;

                }

                else {

                    return n * factorial(n-1);

                }

            }

           

           

            

        }

     

    }

     

    使用递归时必须要有明确的递归结束条件即递归出口否则造成死锁

    递归包含边界条件,递归前进段,递归返回段,当边界条件不满足时递归前进,当边界条件满足时递归返回。递归每一次调用都会生成新的栈帧因此容易造成溢出内存,同时递归运行效率较低。

    5方法的重载中包含参数顺序的 不同,参数修饰符的不同但是返回值不同不能作为区别方法重载的条件

    6静态方法:
    不属于特定的对象属于类方法,只能使用类名.静态方法的形式调用静态方法,不能使用对象调用静态方法。

    只能访问静态变量不能访问实例变量

    不用创建类的对象就可以访问静态方法

    静态方法中不能使用this关键字引用类的当前实例

    非静态方法可以访问静态成员变量也可以访问非静态成员变量同时可以调用静态方法,在实例方法中调用静态方法很常见类似于调用工具方法。

  • 相关阅读:
    Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法
    ubuntu 关闭显示器的命令
    ubuntu android studio kvm
    ubuntu 14.04版本更改文件夹背景色为草绿色
    ubuntu 创建桌面快捷方式
    Ubuntu 如何更改用户密码
    ubuntu 14.04 返回到经典桌面方法
    ubuntu 信使(iptux) 创建桌面快捷方式
    Eclipse failed to get the required ADT version number from the sdk
    Eclipse '<>' operator is not allowed for source level below 1.7
  • 原文地址:https://www.cnblogs.com/moonfans/p/2780414.html
Copyright © 2011-2022 走看看