C#-参数类型以及this关键字用途

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10, b = 12;

            int s1 = Program.sum1(a, b);//静态方法调用

            Program c = new Program();//实例方法调用
            int s2=c.sum2(a, b);
        }

        #region
        public static int sum1(int x,int y)//静态方法
        { return x + y; }
        public int sum2(int x,int y)//实例方法
        { return x + y; }
        #endregion

        #region
        /*形参和实参前均使用关键字out
  out隐含return(通过变量返回),故方法的返回类型为void
  输出型参数也是将内存地址传递给方法
  与引用型参数的区别是调用方法前无需对变量进行初始化
*/
        public static void Cal(int x,int y,out int add)
        {
            add = x + y;
        }
        #endregion

        #region
        //引用型--以ref修饰符声明;把实参的地址传递给形参,形参的值发生改变时,同时改变实参的值.
        public static void Swap(ref int x,ref int y)
        {
            x = x ^ y;
            y = x ^ y;
            x = x ^ y;
        }
        #endregion

        #region
        //值参数--不含任何修饰符;实参的值不受影响.
        public static int getNum(int x)
        {
            return x >= 0 ? x : Math.Abs(x);
        }
        #endregion

        #region
        /*参数数组必须放在整个参数列表的最后,同时参数数组不允许是多维数组。*/
        public static void GetMax(out int max,params int[] a)
        {
            max = a[0];
            for(int i=0;i<a.Length;i++)
            {
                if (a[i] > max)
                    max = a[i];
            }
        }
        #endregion
    }
    class FirstEmployee
    {
        /*使用this解决局部变量与域同名的问题*/
        private string name, sex;
        int age;
        public FirstEmployee (string name,int age)
        {
            //this.name表示域变量,name表示参数变量
            this.name = name;
            this.age = age;
        }
    }
    class SecondEmployee
    {
        //在构造函数中中用this调用另一构造函数
        private string name, sex;
        int age;
        public SecondEmployee (string name,int age)
        {
            //this.name表示域变量,name表示参数变量
            this.name = name;
            this.age = age;
        }
        //调用另一构造函数初始化name和age
        public SecondEmployee(string name,int age,string sex):this(name,age)
        {
            this.sex = sex; 
        }
    }
    #region
    class ThirdEmployee
    {
        //this可以指代类本身的实例
        public string Name{get;set;}
        public string Salary { get; set; }
        public void Save()
        {
            DataStorage.Store(this);
        }

    }
    class DataStorage
    {
        public static void Store(ThirdEmployee employee)
        {

        }
    }
    #endregion
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值