当前位置: 移动技术网 > IT编程>开发语言>c# > C#函数(构造函数)的重载

C#函数(构造函数)的重载

2019年11月20日  | 移动技术网IT编程  | 我要评论
using system;

namespace test
{
    class program
    {
        static void main(string[] args)
        {
            cat cat = new cat();//不含参数的构造方法
            console.writeline("姓名是{0},年龄是{1}",cat.name,cat.age);
            cat cat1 = new cat("一只猫");//含1个参数的构造方法
            console.writeline("姓名是{0},年龄是{1}", cat1.name, cat1.age);
            cat cat2 = new cat("又一只猫",18);//含2个参数的构造方法
            console.writeline("姓名是{0},年龄是{1}", cat2.name, cat2.age);
        }
        class cat
        {
            public string name;
            public int age;
            public cat() { }
            public cat(string namevalue)
            {
                name = namevalue;
            }
            public cat(string namevalue, int agevalue)
            {
                name = namevalue;
                age = agevalue;
            }
        }
    }
}

 

 

using system;

namespace test
{
    class program
    {
        static void main(string[] args)
        {
            int c = calculate.divide(7, 3);
            console.writeline(c);
            double d = calculate.divide(7, 3.0);
            console.writeline(d);
        }
        /// <summary>
        /// 方法名称一样 参数类型不一样 构成重载
        /// </summary>
        class calculate
        {
            public static int divide(int a,int b) {
                return a / b;
            }
            public static double divide(double a, double b)
            {
                return a / b;
            }
        }
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网