다이아몬드 문제(Diamond Problem)
설계의 복잡성 증가
이름 충돌과 충돌 해결의 어려움
설계의 일관성과 단순성 유지
코드의 재사용성
다중 상속 제공
유연한 설계
interface IMyInterface
{
void Method1();
int Method2(string str);
}
class MyClass : IMyInterface
{
public void Method1()
{
// 구현
}
public int Method2(string str)
{
// 구현
return 0;
}
}
// 1. 인터페이스 구현해보기
public interface Imovable // 이동 메서드 정의
{
void Move(int x, int y); // 이동 메서드 선언
}
// 2. 인터페이스를 구현하는 클래스 생성하기
public class Player : Imovable
{
public void Move(int x, int y)
{
// 플레이어의 이동 구현
}
}
// 2. 인터페이스를 구현하는 클래스 생성하기
public class Enemy : Imovable
{
public void Move(int x, int y)
{
// 적의 이동 구현
}
}
static void Main(string[] args)
{
// 3. 인터페이스를 사용하여 객체 이동하기
Imovable movableObject1 = new Player();
Imovable movableObject2 = new Enemy();
movableObject1.Move(1, 2); // 플레이어 이동
movableObject2.Move(1, 9); // 적 이동
}
// 아이템을 사용할 수 있는 인터페이스
public interface IUsable
{
void Use();
}
// 아이템 클래스
public class Item : IUsable
{
public string Name {get; set;}
public void Use()
{
Console.WriteLine("아이템 {0}을 사용했습니다.", Name);
}
}
// 플레이어 클래스
public class Player
{
public void UseItem(IUsable item)
{
item.Use();
}
}
// 게임 실행
static void Main(string[] args)
{
Player player = new Player();
Item item = new Item() { Name = "Health Positon"};
player.UseItem(item);
}
// 인터페이스 1
public interface IItemPickable
{
void PickUp();
}
// 인터페이스 2
public interface IDroppable
{
void Drop();
}
// 아이템 클래스
public class Item : IItemPickable, IDroppable
{
public string Name { get; set; }
public void PickUp()
{
Console.WriteLine("아이템 {0}을 주웠습니다.", Name);
}
public void Drop()
{
Console.WriteLine("아이템 {0}을 버렸습니다.", Name);
}
}
// 플레이어 클래스
public class Player
{
public void InteractWithItem(IItemPickable item)
{
item.PickUp();
}
public void DropItem(IDroppable item)
{
item.Drop();
}
}
// 게임 실행
static void Main()
{
Player player = new Player();
Item item = new Item { Name = "Sword" };
// 아이템 주울 수 있음
player.InteractWithItem(item);
// 아이템 버릴 수 있음
player.DropItem(item);
}
인터페이스의 특징과 장단점
추상 클래스의 특징과 장단점
가독성
자기 문서화(Self-documenting)
스위치 문과의 호환성
// 열거형 정의
enum MyEnum
{
Value1,
Value2,
Value3
}
// 열거형 사용
MyEnum myEnum = MyEnum.Value1;
// 열거형 상수 값 지정
enum MyEnum
{
Value1 = 10,
Value2,
Value3 = 20
}
// 열거형 형변환
int intValue = (int)MyEnum.Value1; // 열거형 값을 정수로 변환
MyEnum enumValue = (MyEnum)intValue; // 정수를 열거형으로 변환
// 스위치문과의 사용
switch(enumValue)
{
case MyEnum.Value1:
// Value1에 대한 처리
break;
case MyEnum.Value2:
// Value2에 대한 처리
break;
case MyEnum.Value3:
// Value3에 대한 처리
break;
default:
// 기본 처리
break;
}
enum DaysOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
class Program
{
static void Main(string[] args)
{
DaysOfWeek day = DaysOfWeek.Monday;
Console.WriteLine("Today is " + day);
}
}
// 월 열거형
public enum Month
{
Jan = 1,
Feb,
Mar,
Apr,
May,
Jun,
Jul,
Aug,
Sep,
Out,
Nov,
Dec
}
// 처리하는 함수
public static void ProcessMonth(int month)
{
if (month >= (int)Month.Jan && month <= (int)Month.Dec)
{
Month selectedMonth = (Month)month;
Console.WriteLine("선택한 월은 {0}입니다.", selectedMonth);
// 월에 따른 처리 로직 추가
}
else
{
Console.WriteLine("올바른 월을 입력해주세요.");
}
}
// 실행 예제
static void Main(string[] args)
{
int userInput = 7; // 사용자 입력 예시
ProcessMonth(userInput);
}
// 게임 상태
enum GameState
{
MainMenu,
Playing,
Paused,
GameOver
}
// 방향
enum Direction
{
Up,
Down,
Left,
Right
}
// 아이템 등급
enum ItemRarity
{
Common,
Uncommon,
Rare,
Epic
}