0311 Class+맴버 변수+메서드 호출 연습
2021. 3. 11. 18:24ㆍC#/과제
public class Carrigon
{
public string name;
public float hp;
public Carrigon()
{
Console.WriteLine("캐리건이 생성되었습니다.");
}
public void SetName(string name)
{
this.name = name;
Console.WriteLine("캐리건의 아이디가 {0}으로 설정되었습니다.", this.name);
}
public void Hp(float hp)
{
this.hp = hp;
Console.WriteLine("캐리건의 데미지가{0}로 설정되었습니다.",this.hp);
}
class Program
{
//캐리건 클래스 호출 후 메서드 호출
static void Main(string[] args)
{
Carrigon carrigon = new Carrigon();
carrigon.SetName("홍길동");
carrigon.Hp(100);
}
}
public class Carrigon
{
public string name;
public float damage;
public Carrigon()
{
Console.WriteLine("캐리건 생성자 호출");
}
public void SetName(string name)
{
this.name = name;
Console.WriteLine("캐리건 아이디가 {0}로 생성되었습니다.", this.name);
}
public string GetName(string name)
{
Console.WriteLine("캐리건 아이디가 {0}로 생성되었습니다.", this.name);
return this.name;
}
}
class Program
{
//캐리건 클래스 호출 후 메서드 호출
static void Main(string[] args)
{
Carrigon carrigon = new Carrigon();
carrigon.SetName("홍길동");
carrigon.GetName("홍길동");
}
}
※해결
public class Gateway
{
public enum eUnitType { ZEALOT,DRAGON }
public Gateway()
{
}
public Unit CreateUnit(eUnitType unitType)
{
switch (unitType)
{
case eUnitType.ZEALOT:
return new Zealot();
case eUnitType.DRAGON:
return new Zealot();
default:
return null;
}
}
}
public class Unit
{
private float x;
private float y;
public Unit() //기본 생성자
{
Console.WriteLine("Unit 생성자 호출됨");
}
public float[] GetPosition()
{
float[] arr = new float[2];
arr[0] = this.x;
arr[1] = this.y;
return arr;
}
public void SetPosition(float x, float y)
{
this.x = x;
this.y = y;
}
}
public class Zealot:Unit
{
public Zealot()
{
Console.WriteLine("질럿이 생성되었습니다.");
}
}
public class Dragon:Unit
{
public Dragon()
{
Console.WriteLine("드라군이 생성되었습니다.");
}
}
class Program
{
static void Main(string[] args)
{
Gateway gateway = new Gateway();
Unit unit = gateway.CreateUnit(Gateway.eUnitType.DRAGON);
Console.WriteLine(unit);
}
}
public class Tomerry
{
public int hp=300;
public Tomerry()
{
}
public void AttackBy(Librarian target)
{
Console.WriteLine("토머리가 사서에게 {0}만큼 데미지를 받습니다.", target.Attack());
while(hp>0)
{
hp -= target.Attack();
}
Console.WriteLine("토머리 접대 완료");
}
public int Resurrect(Resident target)
{
if (target.Die())
{
this.AttackBy(Librarian target);
}
Console.WriteLine("토머리가 사랑 마을 주민을 부활시킵니다.");
hp = hp + 5;
return hp;
}
public void Die()
{
Console.WriteLine("토머리가 죽었습니다.");
}
}
public class Librarian
{
int damage;
public Librarian()
{
Random rand = new Random();
damage = rand.Next(1, 6);
}
public int Attack()
{
return damage;
}
public class Resident
{
public Resident()
{
}
public void Die()
{
}
}
class Program
{
static void Main(string[] args)
{
Tomerry tomerry = new Tomerry();
Librarian librarian = new Librarian();
Resident resident = new Resident();
tomerry.AttackBy(librarian);
}
}
토머리 ToMerry
체력 hp 300
====================
공격당한다 (사서) AttackBy(librarian targer)
부활시킨다 (마을 주민) Resurrect(resident)
죽는다 () Die
사서 Librarian
공격력 1~5
사랑마을 주민 4명과 함께 등장
사랑마을 주민이 죽으면 피통 +5상승
마을 주민이 죽으면 부활시킴
'C# > 과제' 카테고리의 다른 글
0316 컬랙션 과제 (0) | 2021.03.17 |
---|---|
0314 클래스와 메서드 (0) | 2021.03.15 |
0310 메서드 선언/정의 연습 (0) | 2021.03.10 |
0309 반복문 연습 + 랜덤번호 생성 (0) | 2021.03.09 |
0308 데이터 타입과 문자열 표기식 (0) | 2021.03.08 |