오버로딩이란 이름은 같고, 매개변수는 다른 메서드를 만드는 것은 오버로딩(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
*/
'C# > 클래스와 객체지향' 카테고리의 다른 글
C# 클래스와 객체지향 - 07. 생성자와 소멸자 (2) | 2024.05.28 |
---|---|
C# 클래스와 객체지향 - 06. 접근 제한자 (0) | 2024.05.28 |
C# 클래스와 객체지향 - 04. 메서드 (0) | 2024.05.28 |
C# 클래스와 객체지향 - 03. 추상화 (0) | 2024.05.28 |
C# 클래스와 객체지향 - 02. 클래스(사용자 정의 자료형) (1) | 2024.05.28 |