蓝鸥Unity开发基础——方法重载

蓝鸥Unity开发基础——方法重载

一、方法重载

方法重载——多个不同的方法采用同样的名字

方法名相同,参数列表不同

1、未使用方法重载时候——两个整数和,两个小数和

using System;

namespace Lesson11
{
    public  class Person{
    public  int   Sum(int a,int b){
        return a+b;

    }
    public  float SumFloat(float a, float b){
        return a+b;
    }

    }
    class MainClass
    {
        public static void Main (string[] args)
        {

            Person p = new Person ();

            Console.WriteLine (p.SumFloat(3.5f,7.1f));
        }
    }
}

 

2、方法重载代码演练:两个整数和,两个小数和,三个整数和

 

using System;

namespace Lesson11
{
    public  class Person{
        //使用方法重载可以使方法调用更加方便——方法名一样的多个方法

        //方法名必须一样
        //方法中参数列表必须不同

        //1、参数类型不同
        //2、参数个数不同

        //在方法重载中,与返回值类型无关的


        //求两个小数和
    public  int   Sum(int a,int b){
        return a+b;

    }
        //求三个整数的和
        public  int Sum(int a,int b,int c){
        return a+b;
    }
        //求两个小数和
    public  float Sum(float a, float b){
            return a+b;
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {

            Person p = new Person ();

            Console.WriteLine (p.Sum(3.5f,7.1f));
            //系统是根据给方法传递的实际参数来进行判断的
            Console.WriteLine (p.Sum(3,7,11));
        }
    }
}

 

练习:创建重载方法,求两个数的乘积,参数类型分别为int float double。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值