
사진 설명을 입력하세요.
어제 구황작물 넣은 형식을 조금 수정해서, 위와 같은 클래스를 추가하였다.
class PocketMon
{
// 프라이빗 변수
private string number;
private string Name;
private int HP;
private int Attack;
private int Depence;
private int SpecialAT;
private int SpecialDP;
private int Speed;
private Dictionary<int, string> List = new Dictionary<int, string>()
{
{0, "뒤로가기"},
{1, "망나뇽"},
{2, "마기라스"},
{3, "보만다"},
{4, "메타그로스"},
{5, "한카리아스"},
{6, "삼삼드래"},
{7, "미끄래곤"},
{8, "짜랑고우거"},
{9, "드래펄트"},
{10, "드닐레이브"},
{11, "브리두라스"}
};
// 생성자
public PocketMon()
{
this.number = "";
this.Name = "선택되지 않음";
this.HP = 0;
this.Attack = 0;
this.Depence = 0;
this.SpecialAT = 0;
this.SpecialDP = 0;
this.Speed = 0;
}
// 메소드
public void Menu()
{
Console.WriteLine($"보고 싶은 메뉴를 선택해");
Console.WriteLine($"-------------------------");
foreach (KeyValuePair<int, string> item in List)
{
Console.WriteLine("[{0}:{1}]", item.Key, item.Value);
}
Console.WriteLine($"-------------------------");
}
public virtual void PocketMon_info()
{
Console.WriteLine($"포켓몬 이름 : {Name}");
Console.WriteLine($"HP : {HP}");
Console.WriteLine($"공격 : {Attack}");
Console.WriteLine($"방어 : {Depence}");
Console.WriteLine($"특수공격 : {SpecialAT}");
Console.WriteLine($"특수방어 : {SpecialDP}");
Console.WriteLine($"스피드 : {Speed}");
}
public string PocketMon_choice()
{
while (true)
{
number = Console.ReadLine();
foreach (int Key in List.Keys)
{
string stringKey = Key.ToString();
if (stringKey == number)
{
return (string)List[Key];
}
}
Console.WriteLine("다시 선택");
}
}
}
class Dragonite : PocketMon
{
// 프라이빗 변수
private string Name;
private int HP;
private int Attack;
private int Depence;
private int SpecialAT;
private int SpecialDP;
private int Speed;
// 생성자
public Dragonite()
{
this.Name = "망나뇽";
this.HP = 91;
this.Attack = 134;
this.Depence = 95;
this.SpecialAT = 100;
this.SpecialDP = 100;
this.Speed = 80;
}
// 메소드
public override void PocketMon_info()
{
Console.WriteLine($"포켓몬 이름 : {Name}");
Console.WriteLine($"HP : {HP}");
Console.WriteLine($"공격 : {Attack}");
Console.WriteLine($"방어 : {Depence}");
Console.WriteLine($"특수공격 : {SpecialAT}");
Console.WriteLine($"특수방어 : {SpecialDP}");
Console.WriteLine($"스피드 : {Speed}");
}
}
예시로 부모 클래스와 자식 클래스 하나를 펼쳐서 확인하면 다음과 같다.
else if (selected == "600족 포켓몬")
{
while (true)
{
mon.Menu();
mon.PocketMon_info();
string mon_selected = mon.PocketMon_choice();
if (mon_selected == "뒤로가기")
{
mon = new PocketMon(); // 부모 클래스 동적할당 해줌
break;
}
else if (mon_selected == "망나뇽")
{
mon = new Dragonite(); // 고구마 데이터 동적할당
}
else if (mon_selected == "마기라스")
{
mon = new Tyranitar(); // 감자 데이터 동적할당
}
else if (mon_selected == "보만다")
{
mon = new Salamence(); // 옥수수 데이터 동적할당
}
else if (mon_selected == "메타그로스")
{
mon = new Metagross(); // 콩 데이터 동적할당
}
else if (mon_selected == "한카리아스")
{
mon = new Garchomp(); // 조 데이터 동적할당
}
else if (mon_selected == "삼삼드래")
{
mon = new Hydreigon(); // 토란 데이터 동적할당
}
else if (mon_selected == "미끄래곤")
{
mon = new Goodra(); // 칡 데이터 동적할당
}
else if (mon_selected == "짜랑고우거")
{
mon = new Kommo_o(); // 메밀 데이터 동적할당
}
else if (mon_selected == "드래펄트")
{
mon = new Dragapult(); // 메밀 데이터 동적할당
}
else if (mon_selected == "드닐레이브")
{
mon = new Baxcalibur(); // 메밀 데이터 동적할당
}
else if (mon_selected == "브리두라스")
{
mon = new Archaludon(); // 메밀 데이터 동적할당
}
}
}
}
}
}
}
메인 부분 선택지 부분에도 코드를 추가해준다.
들여쓰기가 많이 추가돼서 슬슬 불편한데, 함수를 쓰지 않아서 감수해야할 부분이다.
만약 함수를 추가한다면 매개변수에 클래스를 포인터로 전달해줘야 한다.
아니면 다른 방법을 고안해본게, 클래스 this() 생성자를 이용해서 클래스 안에서 else if 문과 같은 구조를 굴리는 것도 좋을 것 같은데 이미 이런 형태로 만들고 있어서 상상으로만 그쳤다.
실행결과




'C#' 카테고리의 다른 글
LMS5_Project08 C# WPF 프로젝트 - Resource Monitoring Tool (0) | 2025.02.22 |
---|---|
C# WPF 데이터베이스 연결 (0) | 2025.02.05 |
C# 문제풀이 2 (0) | 2025.01.24 |
C# 문제풀이 1 (3) | 2025.01.24 |
01.21 학습일지 및 과제 현황 (0) | 2025.01.21 |