인터페이스와 열거형
개인 과제 해설 및 피드백
학습법 특강
ex) USB : A타입 핀이 4개 / 2개는 입력 & 2개는 출력
// 인터페이스 및 멤버 정의
interface IMyInterface
{
void Method1();
int Method2(string str);
}
// 인터페이스 구현
class MyClass : IMyInterface
{
public void Method1()
{
// 구현
}
public int Method2(string str)
{
// 구현
return 0;
}
}
// 이동 구현 예제
internal class Program
{
public interface IMovable
{
void Move(int x, int y); // 이동 메서드 선언
}
public class Player : IMovable
{
public void Move(int x, int y)
{
// 이동 구현
}
}
public class Enemy : IMovable
{
public void Move(int x, int y)
{
// 이동 구현
}
}
static void Main(string[] args)
{
IMovable movableObject1 = new Player();
IMovable movableObject2 = new Enemy();
movableObject1.Move(1, 2); // 플레이어 이동값
movableObject2.Move(1, 9); // 적 이동값
}
}
// 아이템 사용 예제
internal class Program
{
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()
{
Player player = new Player();
Item item = new Item { Name = "Health Potion" };
player.UseItem(item);
}
}
// 다중 상속 구현 예제
internal class Program
{
// 인터페이스 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);
}
}
// 열거형 정의
enum MyEnum
{
Value1,
Value2,
Value3
}
//열거형 사용
MyEnum myEnum = MyEnum.Value1;
// 열거형 상수 값 지정
enum MyEnum
{
Value1 = 10,
Value2, // Value1 의 다음 값인 11이 지정
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,
Oct,
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
}
// LINQ 사용 X 방법
int totalAtk = 0;
for (int i = 0; i < inventory.Count; i++)
{
totalAtk += inventory[i].Atk;
}
// LINQ 사용 O 방법
inventory.Select(item => item.Atk).Sum();

코딩하기
Document 확인하기
공유하기
Cording Test 해보기
튜터님에게 질문하기
TIL로 정리하기
문제를 마주칠수록 성장할 가능성이 높아진다!