해당 문서는 인프런 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문을 듣고 정리한 필기 노트입니다.
은닉성 / 상속성 / 다형성
class Knight
{
public int hp;
public int attack;
public Knight()
{
hp = 100;
attack = 10;
Console.WriteLine("생성자 호출");
}
public Knight(int hp) : this() // default 버전 먼저 호출
{
this.hp = hp; // this : 내 hp
}
public Knight(int hp, int attack) : this(hp)
{
this.hp = hp;
this.attack = attack;
}
}
class Program
{
static void Main(string[] args)
{
Knight knight= new Knight(); // 생성자
Knight knight2= new Knight(50);
Knight knight3= new Knight(50, 5);
}
}
인스턴스가 아닌 객체에 종속
class Knight
{
static public int a; // Knight에 종속. 오로지 1개만 존재 (모든 인스턴스가 공유)
public int hp;
public int attack;
static public void CreateKnight()
{
// static 내에서는 static 변수만 접근 가능
}
public void Move() ...
}
class Program
{
static void Main(string[] args)
{
Knight knight = Knight.CreateKnight(); // 객체에 종속적이기 때문에 Knight에서 바로 불러올 수 있음
knight.Move(); // 인스턴스에 종속
}
}
부모의 field 뿐만 아니라 method도 가져올 수 있음
class Player // 부모
{
static public int counter = 1;
public int id;
public int hp;
public int attack;
public Player()
{
Console.WriteLine("Player 생성자");
}
public Player(int hp)
{
this.hp = hp;
Console.WriteLine("Player hp 생성자");
}
}
class Knight : Player // 자식
{
// 부모 생성자 먼저 생성
// 후 자식 생성자 생성
public Knight() : base(100) // 부모 생성자 버전 명시 가능
{
base.hp = 100; // 부모 변수 접근
Console.WriteLine("Knight 생성자)";
}
}
class Player // 부모
{
static public int counter = 1;
public int id;
public int hp;
public int attack;
public Player()
{
Console.WriteLine("Player 생성자");
}
public void Move()
{
Console.WriteLine("Player 이동");
}
}
class Mage : Player
{
public int hp;
public new void Move() // 부모 함수 무시하고 이거 사용
{
Console.WriteLine("Mage 이동");
}
}
class Knight : Player
{
public new void Move()
{
Console.WriteLine("Knight 이동");
}
}
class Program
{
static void EnterGame(Player player)
{
// 방법 1
bool isMage = (player is Mage);
if (isMage)
{
Mage mage = (Mage)player;
mage.mp = 10;
}
// 방법 2 (아예 캐스팅까지)
Mage mage = (player as Mage);
if (mage != null)
{
mage.mp = 10;
mage.Move(); // 무식한 방법 -> 다형성 등장
}
}
static void Main(string[] args)
{
Player player = new Knight(); // 이런 것도 가능!
Knight knight = new Knight();
Mage mage = new Mage();
EnterGame(knight);
EnterGame(mage);
}
}
virtual / override 이용
class Player
{
public Player()
{
Console.WriteLine("Player 생성자");
}
public virtual void Move()
{
Console.WriteLine("Player 이동");
}
}
class Mage : Player
{
public int hp;
public override void Move()
{
Console.WriteLine("Mage 이동");
}
}
class Knight : Player
{
public sealed override void Move() // sealed : Knight를 상속받는 자식들은 더이상 override 불가능
{
base.Move();
Console.WriteLine("Knight 이동");
}
}
class Program
{
static void EnterGame(Player player)
{
player.Move(); // 런타임에 어떤 타입의 변수였는지 확인해서 해당 타입에 맞는 함수 호출
}
static void Main(string[] args)
{
Knight knight = new Knight();
Mage mage = new Mage();
knight.Move();
mage.Move();
EnterGame(mage); // output : Mage 이동
}
}
string name = "Harry Potter";
bool found = name.Constains("Harry");
int index = name.IndexOf("P"); // 특정 문자가 몇번째에 있는지 확인. 없으면 -1
name = name + " Junior"; // 문자열 추가
string lowerCaseName = name.ToLower(); // 모두 소문자로 변환
string upperCaseName = name.ToUpper(); // 모두 대문자로 변환
string newName = name.Replace('r', 'l'); // r을 l로 치환
// 띄어쓰기 기준으로 분할하여 names에 저장
string[] names = name.Split(new char[] { ' ' });
// 5번 index부터 분할
string substringName = name.Substring(5);