- 주로 클래스 내부의 멤버변수들은 private 로 해주고 함수를 public 으로 하여 멤버변수에 접근하게 사용


ex) GameObject 클래스 생성
class GameObject
{
int x;
int y;
int z;
}
static void Main(string[] args)
{
GameObject gameObject = new GameObject();
gameObject. ---
- ✅ 일반적으로 아무 접근 지정자도 설정하지 않은 경우 private로 되어있음
- private로 잡혀있기 때문에 gameObject객체를 통해 값으로 접근 불가
public : 클래스 내부 데이터를 외부에서도 접근을 허용하는 지정자
protected : 클래스 내부와 자기가 상속하고 있는 클래스에게만 접근을 허용하는 지정자
private : 클래스 내부의 데이터를 내부에서만 사용할 수 있게 허용하는 지정자

ex) GameObject 클래스 생성
class GameObject
{
private int x;
private int y;
private int z;
}
static void Main(string[] args)
{
GameObject gameObject1 = new GameObject();
GameObject gameObject2 = new GameObject();
GameObject gameObject3 = new GameObject();
}
class GameObject
{
private int x;
private int y;
private int z;
public void Position(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
- GameObject 클래스 내부에 Position( ) 함수 생성
📌 this Pointer : 자기 자신을 가리키는 포인터
- -> 여기서 this.x this.y this.z 는 클래스 내부에 선언한 멤버변수
- 즉, 맴버 변수를 매개 변수로 들어온 값으로 변경
public void Information()
{
Console.WriteLine("x의 값 : " + x);
Console.WriteLine("y의 값 : " + y);
Console.WriteLine("z의 값 : " + z);
Console.WriteLine();
}
static void Main(string[] args)
{
GameObject gameObject1 = new GameObject();
GameObject gameObject2 = new GameObject();
GameObject gameObject3 = new GameObject();
gameObject1.Position(5, 5, 5);
gameObject2.Position(2, 2, 2);
gameObject3.Position(1, 1, 1);
gameObject1.Information();
gameObject2.Information();
gameObject3.Information();
}
결과값 :
생성자 : 접근지정자 + 클래스 이름 ( )
class Unit
{
public int health;
public int attack;
public Unit() // 생성자
{
Console.WriteLine("생성자");
}
}
static void Main(string[] args)
{
Unit unit = new Unit();
}
결과값 :
ex) Vitamin 클래스 생성
class Vitamin
{
private float b6;
private float c;
private double d;
}

class Vitamin
{
private float b6;
private double d;
private float c;
}

✅ 클래스에 멤버 변수를 선언하는 경우 : 작은 자료형부터 큰 자료형 순으로 선언하기!
ex) Unit 클래스 생성
class Unit
{
public int health;
public int attack;
public Unit() // 생성자
{
Console.WriteLine("생성자");
}
// 복사 생성자 정의
public Unit(Unit clone)
{
Console.WriteLine("복사 생성자");
}
}

// 생성자
Unit marine1 = new Unit();
// 복사 생성자
Unit marine2 = new Unit(marine1);
marine1.health = 10;
marine1.attack = 50;
marine2.health = marine1.health;
marine2.attack = marine1.attack;
Console.WriteLine("marine2의 health 값: " + marine2.health);
Console.WriteLine("marine2의 attack 값: " + marine2.attack);
결과값 :
(2024.11.25.월요일)