절차(procedure) 지향 : 함수 기반 / 프로그램이 커질 수록 유지보수 측면에서 어려움을 겪을 수 있다. (함수 호출 순서가 중요하다)
객체(OOP Object Oriented Programming) 지향 : 모든 것을 객체 우선으로 생각한다. (모든 코드들을 객체라고 한다.)
객체를 설계하기 위해선 어떠한 방식으로든 객체를 묘사해야 한다.
객체는 속성과 기능으로 묘사할 수 있다.
예) 기사라는 직업 (속성 : hp, attack, pos...), (기능 : Move, Attack, Die ... / 할 수 있는 모든 것)
객체를 묘사하고 싶을 때 쓰는 문법은 class
class는 객체의 틀 (붕어빵 틀과 같이 재료만 넣으면 객체를 만들어 내는 틀)
public으로 선언해줘야 클래스를 불러올 때 외부에서도 사용할 수 있게 된다. (public이 없을 경우 클래스 내에서만 사용할 수 있다.)
new 문법을 통해서 객체를 생성할 수 있다.
using System;
namespace Csharp
{
class Knight
{
public int hp;
public int attack;
public void Move()
{
Console.WriteLine("Knight Move");
}
public void Attack()
{
Console.WriteLine("Knight Attack");
}
}
class Program
{
static void Main(string[] args)
{
Knight knight = new Knight();
knight.hp = 100;
knight.attack = 10;
knight.Move();
knight.Attack();
}
}
}
using System;
namespace Csharp
{
class Knight
{
public int hp;
public int attack;
public void Move()
{
Console.WriteLine("Knight Move");
}
public void Attack()
{
Console.WriteLine("Knight Attack");
}
}
struct Mage
{
public int hp;
public int attack;
}
class Program
{
static void KillMage(Mage mage)
{
mage.hp = 0;
}
static void KillKnight(Knight knight)
{
knight.hp = 0;
}
static void Main(string[] args)
{
Mage mage;
mage.hp = 100;
mage.attack = 50;
KillMage(mage);
Knight knight = new Knight();
knight.hp = 100;
knight.attack = 10;
KillKnight(knight);
}
}
}
using System;
namespace Csharp
{
class Knight
{
public int hp;
public int attack;
public Knight Clone()
{
Knight knight = new Knight();
knight.hp = hp;
knight.attack = attack;
return knight;
}
public void Move()
{
Console.WriteLine("Knight Move");
}
public void Attack()
{
Console.WriteLine("Knight Attack");
}
}
struct Mage
{
public int hp;
public int attack;
}
class Program
{
static void KillMage(Mage mage)
{
mage.hp = 0;
}
static void KillKnight(Knight knight)
{
knight.hp = 0;
}
static void Main(string[] args)
{
Mage mage;
mage.hp = 100;
mage.attack = 50;
KillMage(mage);
Knight knight = new Knight();
knight.hp = 100;
knight.attack = 10;
Knight knight2 = knight.Clone();
}
}
}

코드(code) 영역
메모리의 코드(code) 영역은 실행할 프로그램의 코드가 저장되는 영역으로 텍스트(code) 영역이라고도 부릅니다.
CPU는 코드 영역에 저장된 명령어를 하나씩 가져가서 처리하게 됩니다.
데이터(data) 영역
메모리의 데이터(data) 영역은 프로그램의 전역 변수와 정적(static) 변수가 저장되는 영역입니다.
데이터 영역은 프로그램의 시작과 함께 할당되며, 프로그램이 종료되면 소멸합니다.
스택(stack) 영역
메모리의 스택(stack) 영역은 함수의 호출과 관계되는 지역 변수와 매개변수가 저장되는 영역입니다.
스택 영역은 함수의 호출과 함께 할당되며, 함수의 호출이 완료되면 소멸합니다.
이렇게 스택 영역에 저장되는 함수의 호출 정보를 스택 프레임(stack frame)이라고 합니다.
스택 영역은 푸시(push) 동작으로 데이터를 저장하고, 팝(pop) 동작으로 데이터를 인출합니다.
이러한 스택은 후입선출(LIFO, Last-In First-Out) 방식에 따라 동작하므로, 가장 늦게 저장된 데이터가 가장 먼저 인출됩니다.
스택 영역은 메모리의 높은 주소에서 낮은 주소의 방향으로 할당됩니다.
힙(heap) 영역
메모리의 힙(heap) 영역은 사용자가 직접 관리할 수 있는 ‘그리고 해야만 하는’ 메모리 영역입니다.
힙 영역은 사용자에 의해 메모리 공간이 동적으로 할당되고 해제됩니다.
힙 영역은 메모리의 낮은 주소에서 높은 주소의 방향으로 할당됩니다.
스택과 힙의 장단점
스택
매우 빠른 액세스
변수를 명시 적으로 할당 해제 할 필요가 없습니다.
공간은 CPU에 의해 효율적으로 관리되고 메모리는 단편화되지 않습니다.
지역 변수 만
스택 크기 제한 (OS에 따라 다름)
변수의 크기를 조정할 수 없습니다.
힙
변수는 전역 적으로 액세스 할 수 있습니다.
메모리 크기 제한 없음
(상대적으로) 느린 액세스
효율적인 공간 사용을 보장하지 못하면 메모리 블록이 할당 된 후 시간이 지남에 따라 메모리가 조각화되어 해제 될 수 있습니다.
메모리를 관리해야합니다 (변수를 할당하고 해제하는 책임이 있습니다)
변수는 C언어 realloc() or 자바 new
using System;
namespace Csharp
{
class Knight
{
public int hp;
public int attack;
public Knight()
{
hp = 100;
attack = 100;
Console.WriteLine("생성자 호출!");
}
public Knight(int hp)
{
this.hp = hp;
}
public Knight Clone()
{
Knight knight = new Knight();
knight.hp = hp;
knight.attack = attack;
return knight;
}
public void Move()
{
Console.WriteLine("Knight Move");
}
public void Attack()
{
Console.WriteLine("Knight Attack");
}
}
class Program
{
static void Main(string[] args)
{
Knight knight = new Knight();
}
}
}
public Knight(int hp) : this()
{
this.hp = hp;
}
class Progrma
{
static void Main(string[], args)
{
}
}
using System;
namespace Csharp
{
class Player
{
static public int counter = 1;
public int id;
public int hp;
public int attack;
public Player()
{
Console.WriteLine("Player 생성자 호출!")
}
}
class Mage : Player
{
}
class Archer : Player
{
}
class Knight : Player
{
Knight()
{
Console.WriteLine("Knight 생성자 호출!");
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
생성자의 경우에도 상위 클래스의 생성자부터 호출이 되고 자식 클래스의 생성자가 호출이 된다.
base 키워드를 통해서 상속 받은 클래스를 가리킬 수 있다. (this는 자신의 클래스의 변수에 접근 / base는 부모 클래스의 변수에 접근)
using System;
namespace Csharp
{
class Player
{
static public int counter = 1;
public int id;
public int hp;
public int attack;
public void Move()
{
Console.WriteLine("Player Move!");
}
public void Attack()
{
Console.WriteLine("Player Attack!");
}
}
class Mage : Player
{
}
class Archer : Player
{
}
class Knight : Player
{
}
class Program
{
static void Main(string[] args)
{
Knight knight = new Knight();
knight.Attack();
}
}
}
using System;
using System.Xml.Serialization;
namespace CSharp
{
class Knight
{
private int hp;
public void SetHp(int hp)
{
this.hp = hp;
}
protected int mp;
}
class SuperKnight : Knight
{
void Test()
{
mp = 10;
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
using System;
using System.Xml.Serialization;
namespace CSharp
{
class Player
{
protected int hp;
protected int attack;
}
class Knight : Player
{
}
class Mage : Player
{
public int mp;
}
class Program
{
static void EnterGame(Player player)
{
// bool isMage = (player is Mage);
Mage mage = (player as Mage);
if (mage != null)
{
mage.mp = 10;
Console.WriteLine("나는 마법사다!");
}
}
static void Main(string[] args)
{
Knight knight = new Knight();
Mage mage = new Mage();
EnterGame(knight);
EnterGame(mage);
}
}
}
using System;
using System.Xml.Serialization;
namespace CSharp
{
class Player
{
protected int hp;
protected int attack;
public void Move()
{
Console.WriteLine("Player 이동");
}
}
class Knight : Player
{
public new void Move()
{
Console.WriteLine("Knight 이동");
}
}
class Mage : Player
{
public int mp;
public new void Move()
{
Console.WriteLine("Mage 이동");
}
}
class Program
{
static void EnterGame(Player player)
{
// bool isMage = (player is Mage);
Mage mage = (player as Mage);
if (mage != null)
{
mage.mp = 10;
Console.WriteLine("나는 마법사다!");
mage.Move();
}
Knight knight = (player as Knight);
if (knight != null)
{
Console.WriteLine("나는 기사다!");
knight.Move();
}
}
static void Main(string[] args)
{
Knight knight = new Knight();
Mage mage = new Mage();
EnterGame(knight);
EnterGame(mage);
}
}
}
using System;
using System.Xml.Serialization;
namespace CSharp
{
class Player
{
protected int hp;
protected int attack;
public virtual void Move()
{
Console.WriteLine("Player 이동");
}
}
class Knight : Player
{
public override void Move()
{
Console.WriteLine("Knight 이동");
}
}
class Mage : Player
{
public int mp;
public override void Move()
{
Console.WriteLine("Mage 이동");
}
}
class Program
{
static void EnterGame(Player player)
{
player.Move();
}
static void Main(string[] args)
{
Knight knight = new Knight();
Mage mage = new Mage();
EnterGame(knight);
EnterGame(mage);
}
}
}
using System;
namespace CSarp
{
class Program
{
static void Main(string[] args)
{
string name = "Harry Potter";
// 1. 찾기
bool found = name.Contains("Harry");
Console.WriteLine(found); // True
int index = name.IndexOf('r');
Console.WriteLine(index); // 가장 먼저 찾는 index / 없으면 -1
// 2.변형
// 2-1 추가
name = name + "Junior";
Console.WriteLine(name);
// 2-2 대소문자 변경
string lowerCaseName = name.ToLower();
string upperCaseName = name.ToUpper();
Console.WriteLine(lowerCaseName + " " + upperCaseName);
string newName = name.Replace('r', 'l');
Console.WriteLine(newName); // Hally PottelJuniol
// 3. 분할
string[] names = name.Split(new char[] { ' ' });
string substringName = name.Substring(5);
Console.WriteLine(substringName); // PotterJunior
}
}
}