Notice
Recent Posts
Recent Comments
Link
스토리지
[3.11] C# 클래스 선언 및 인스턴스 생성 예제 본문
using System;
namespace Study02
{
public class App
{
public App()
{
Console.WriteLine("App생성자");
Hydralisk hydralisk = new Hydralisk();
Console.WriteLine(hydralisk);
Mutalisk mutalisk = new Mutalisk();
Console.WriteLine(mutalisk);
Lurker lurker = new Lurker(120);
Console.WriteLine(lurker);
Drone drone = new Drone();
Console.WriteLine(drone);
Queen queen = new Queen();
Console.WriteLine(queen);
Arbiter arbiter;
arbiter = new Arbiter();
Console.WriteLine(arbiter);
Dragoon dragoon;
dragoon = new Dragoon();
Console.WriteLine(dragoon);
Observatory observatory;
observatory = null;
TemplarArchives templarArchives;
templarArchives = new TemplarArchives();
Console.WriteLine(templarArchives);
Console.WriteLine();
//드라군의 멤버변수는 객체지향의 은닉성을 적용시켜 직접변경 못하게함.
dragoon.setHp(100);
dragoon.setShieldHp(80);
lurker.AttackDragoon(dragoon);
Console.WriteLine();
Dragoon dragoon2 = new Dragoon();
dragoon2.setHp(100);
dragoon2.setShieldHp(80);
Console.WriteLine();
Broodling broodling = queen.makeBroodling();
broodling.damage = 4;
queen.energy = 140;
broodling.AttackDragoon(queen, dragoon);
Console.WriteLine();
Observer observer;
if (observatory == null)
{
//옵저버토리가 없으면
observer = new Observer();
observer = null;
}
else
{
//옵저버토리가 있으면
observer = new Observer(observatory);
}
Console.WriteLine();
dragoon2.AttackLurker(lurker, observer);
Console.WriteLine();
}
}
}
using System;
namespace Study02
{
public class Arbiter
{
public Arbiter()
{
Console.WriteLine("Arbiter 생성됨.");
}
}
}
using System;
namespace Study02
{
public class Observer
{
public Observer()
{
Console.WriteLine("Observatory 건설필요.");
}
public Observer(Observatory observatory)
{
Console.WriteLine("옵저버 생성됨.");
}
}
}
using System;
namespace Study02
{
public class Dragoon
{
private int hp;
private int shieldHp;
private int damage;
private string name;
public Dragoon()
{
Console.WriteLine("Dragoon 생성됨.");
name = "드라군";
}
public int GetHp()
{
return hp;
}
public int GetShieldHp()
{
return shieldHp;
}
public int GetDamage()
{
return damage;
}
public void setHp(int hp)
{
this.hp = hp;
}
public void setShieldHp(int shieldHp)
{
this.shieldHp = shieldHp;
}
public void setAttack(int damage)
{
this.damage = damage;
}
public bool Damaged(int damage)
{
bool isDead = false;
if (shieldHp - damage < 0)
{
this.hp -= damage - this.shieldHp;
this.shieldHp = 0;
if (this.hp < 0)
return !isDead;
return isDead;
}
else if (hp <= 0)
{
Die();
return !isDead;
}
else
{
this.shieldHp -= damage;
return isDead;
}
}
public void AttackLurker(Lurker lurker, Observer observer)
{
if (observer == null)
{
Console.WriteLine("럴커가 버로우 상태이므로 안보여서 공격 불가능");
}
else
{
if(lurker.Damaged(this.damage))
{
Console.WriteLine("럴커가 생존함. 체력 : {0}/{1}", lurker.hp - damage, lurker.hp);
}
else
{
Console.WriteLine("럴커가 사망함.");
}
}
}
public void Die()
{
Console.WriteLine("{0}이(가) 죽었습니다.", name);
}
}
}
using System;
namespace Study02
{
public class Drone
{
public Drone()
{
Console.WriteLine("Drone 생성.");
}
}
}
using System;
namespace Study02
{
public class Hydralisk
{
public Hydralisk()
{
Console.WriteLine("Hydralisk 생성");
}
}
}
using System;
namespace Study02
{
public class Lurker
{
public int groundAttack;
public int hp;
public Lurker(int groundAttack)
{
Console.WriteLine("Lurker 생성.");
this.groundAttack = groundAttack;
}
public void AttackDragoon(Dragoon dragoon)
{
int originDragoonHp = dragoon.GetHp();
Console.WriteLine("럴커가 드라군을 공격합니다.");
if (dragoon.Damaged(this.groundAttack))
{
Console.WriteLine("공격 성공. 드라군 사망");
}
else
{
if (originDragoonHp != dragoon.GetHp())
Console.WriteLine("공격 성공. 드라군 체력(쉴드/본체) : {0}/{1}", dragoon.GetShieldHp(), dragoon.GetHp());
else
Console.WriteLine("공격 실패.");
}
}
public bool Damaged(int damage)
{
return this.hp - damage > 0 ? true : false;
}
}
}
using System;
namespace Study02
{
public class Mutalisk
{
public Mutalisk()
{
Console.WriteLine("Mutalisk 생성됨.");
}
}
}
using System;
namespace Study02
{
public class Queen
{
public int hp;
public int energy;
public Queen()
{
Console.WriteLine("Queen 생성.");
}
public Broodling makeBroodling()
{
return new Broodling();
}
}
}
using System;
namespace Study02
{
public class Observatory
{
public int shieldHp;
public int hp;
public Observatory()
{
Console.WriteLine("Observatory 생성됨.");
}
}
}
using System;
namespace Study02
{
public class TemplarArchives
{
public TemplarArchives()
{
Console.WriteLine("Templar Archives 생성됨.");
}
}
}
using System;
namespace Study02
{
public class Broodling
{
public int hp;
public int damage;
public Broodling()
{
Console.WriteLine("브루들링 생성됨.");
}
public void AttackDragoon(Queen queen, Dragoon dragoon)
{
if (queen.energy >= 150)
{
this.damage *= 2000;
}
Console.WriteLine("퀸에서 만들어진 브루들링이 드라군을 공격함.");
if (dragoon.Damaged(damage))
{
Console.WriteLine("드라군이 죽음.");
}
else
{
Console.WriteLine("드라군이 생존함. 드라군 체력(HP/쉴드HP) : {0}/{1}", dragoon.GetHp(), dragoon.GetShieldHp());
}
}
}
}
using System;
namespace Study02
{
public class Spire
{
public bool isGreatSpire;
public Spire()
{
Console.WriteLine("Spire 건설됨.");
isGreatSpire = false;
}
}
}
using System;
namespace Study02
{
public class Mutalisk
{
public int hp;
public int attack;
public int targetNum;
public Mutalisk(Spire spire)
{
Console.WriteLine("Mutalisk 생성됨.");
}
public Guardian ChangeToGuardian(Spire spire)
{
return spire.isGreatSpire ? new Guardian() : null;
}
}
}
using System;
namespace Study02
{
public class Guardian
{
public int hp;
public int damage;
public Guardian()
{
Console.WriteLine("가디언 생성됨.");
}
}
}
내부 구현은 상위클래스인 Unit과 Building이 설계되어야 가능하다.
모든 건물은 HP를 가지고 있고, 모든 유닛은 HP와 공격력을 가지고 있다. 이것을 각각 클래스에 다 선언하기보다 상위 클래스를 하나 만들어 상속을 시키는 것이 객체지향적인 코드일 것 이다.
'Unity > 수업내용(C#)' 카테고리의 다른 글
[3.12] 메소드의 키워드 (ref, out, params) (0) | 2021.03.12 |
---|---|
[3.12] Call by Value & Call by Reference (0) | 2021.03.12 |
[3.11] Class 생성 및 객체 생성 (0) | 2021.03.11 |
[3.10] 메소드 연습 2 - 반환값만 있는 메소드 (0) | 2021.03.10 |
[3.10] 메소드 연습 1 - 방향판별 (0) | 2021.03.10 |
Comments