c# 方法

方法

namespace ConsoleApp1;
class Program
{
    //方法的声明  需要指定访问修饰符、返回类型、参数列表
    //访问修饰符:决定了变量或方法对于另一个类的可见性
    public  int Function(int n)
    {
        //计算n的阶乘
        if (n > 1)
            return n*Function(n - 1);
        else
            return 1;

    }
    static void Main(string[] args)
    {
        Program proj = new Program();//实例化一个对象

        int a;
        Int64  b;
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("请输入一个整数");
            a = Convert.ToInt32(Console.ReadLine());
            b = proj.Function(a);
            Console.WriteLine("{0}的阶乘是{1}", a, b);
        }

    }

}

namespace ConsoleApp1;
class Program
{
    //方法的声明  需要指定访问修饰符、返回类型、参数列表
    //访问修饰符:决定了变量或方法对于另一个类的可见性
    static  int Function(int n)
    {
        //计算n的阶乘
        if (n > 1)
            return n*Function(n - 1);
        else
            return 1;

    }
    static void Main(string[] args)
    {

        int a;
        Int64  b;
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("请输入一个整数");
            a = Convert.ToInt32(Console.ReadLine());
            b = Function(a);
            Console.WriteLine("{0}的阶乘是{1}", a, b);
        }

    }

}

namespace ConsoleApp1;
class Program
{
    //参数的传递方式

    static void test1(int n)
    {
        //值传递方式
        n = n * n;
        Console.WriteLine("方法内n的值{0}", n);

    }
    static void test2(ref int n)
    { 
        //按引用传递,ref关键字声明参数  
        //引用参数就是对变量内存位置的引用
        n = n * n;
        Console.WriteLine("方法内n的值{0}", n);

    }
    static int test3(params int[] arr)
    {
        //参数数组,当用户不能确定传递参数个数时,可以使用params关键字来定义
        //计算arr的和
        int sum = 0;
        foreach(int i in arr)
        {
            sum+= i;
        }
        return sum;
    }
    static void Main(string[] args)
    {
        int n = 10;
        //Console.WriteLine("main方法内n的值{0}", n);
        //test1(n);
        //test2(ref n);
        //Console.WriteLine("main方法内n的值{0}", n);

        int[] array = { 1, 2, 3, 4, 5 };
        int sum=test3(array);
        Console.WriteLine(sum);
    }

}

namespace ConsoleApp1;
class Program
{
    //方法的分类
    //可以分为静态方法和实例方法
    //静态方法用static修饰,静态方法不对特定实例进行操作,并且只能访问类中的静态成员,访问静态方法可以直接访问
    //实例方法,可以访问静态成员和实例成员,可以通过this访问实例

    int a;  //实例成员
    static int b;//静态成员
    void method() //实例方法  
    {
        a=1;  //等价于this.a=1
        b = 10;
        //在实例方法中,可以访问实例成员,也可以访问静态成员
        Console.WriteLine("a{0}b{1}", a, b);
    }
    static void static_method()//静态方法
    {
        b = 10;//在静态方法中,可以访问静态成员,但是不能访问实例成员
        Console.WriteLine("b{0}",b);
    }

    static void Main(string[] args)
    {
     //静态方法的调用,直接调用,或者使用类名
     Program.static_method();
     //实例方法需要先new个对象
     Program proj=new Program();
      proj.method();
        
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一壶浊酒..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值