C#/클래스와 객체지향
C# 클래스와 객체지향 - 05. 오버로딩
tita
2024. 5. 28. 15:12
오버로딩이란 이름은 같고, 매개변수는 다른 메서드를 만드는 것은 오버로딩(Overloading) 이라고 부릅니다.
오버로딩은 말 그대로 이름은 같고 매개변수가 다른 메서드를 만들기만 하면 되기 때문에 예제를 바로 살펴보겠습니다.
class Program
{
class MyMath
{
public static int Abs(int input)
{
if(input < 0) { return -input; }
else { return input; }
}
public static double Abs(double input)
{
if(input < 0) { return -input; }
else { return input; }
}
public static long Abs(long input)
{
if(input < 0) { return -input; }
else { return input; }
}
}
static void Main(string[] args)
{
// int
Console.WriteLine(MyMath.Abs(12));
Console.WriteLine(MyMath.Abs(-345));
// double
Console.WriteLine(MyMath.Abs(12.123));
Console.WriteLine(MyMath.Abs(-345.123));
// long
Console.WriteLine(MyMath.Abs(21474836470));
Console.WriteLine(MyMath.Abs(-21474836470));
}
}
/*
[실행 결과]
12
324
12.123
345.123
21474836470
21474836470
*/