C#学习笔记。。创建重载方法,求两个数的乘积,参数类型分别为int,float,double

来源:互联网 发布:广发数据签名异常 编辑:程序博客网 时间:2024/06/05 10:58
using System;




//方法重载
//方法重载是由多个不同的方法采用同样的名字,,
//方法重载要满足的条件:  1方法名相同 2不同方法中的参数列表不同
//参数列表不同的表现方式1参数类型不同2参数个数不同
//使用方法重载可以使方法调用更加方便--特点是多个方法名一样
//在方法重载中 与返回值类型无关
namespace Lesson27
{
public class Person{
//求两个整数的和
public int sum(int a ,int b){
return a+b;
}
public float sum(float a,float b){
return a+b;
}
public float sum(float  a,float  b,float c){
return a+b+c;

public int product(int a ,int b){
return a*b;
}
public float product(float a,float b){
return a*b;
}
public double product(double a,double b,double c){
return a*b*c;
}
}


class MainClass
{
public static void Main (string[] args)
{
Person p = new Person();
Console.WriteLine (p.sum(2,7));// 两次调用的不同的方法
Console.WriteLine (p.sum(3.5f,6.7f));
Console.WriteLine (p.sum(3,6.7f));
//在调用操作方法时,系统会根据实际参数的类型或数量来推导出最终调用哪个方法
Console.WriteLine (p.sum(3f,3,6.7f));
Console.WriteLine (p.product(123,4));
Console.WriteLine (p.product(1.228f,4.2f));
Console.WriteLine (p.product(1.833333f,433.77772f,3.555f));
}
}
}
阅读全文
0 0
原创粉丝点击