C#
[유니티로 배우는 C#] 구조체
재코딩
2022. 7. 19. 11:25
반응형
Sturct
구조는 클레스와 동일하지만, 직접 값을 대입시킬 수 없다. 단순히 구조체 역할만 한다.
값을 대입시키기 위해서는 Public을 넣어주거나 parameter를 받아 값을 넣어줄 수 있다.
상속이 불가능하다. class의 구버전이라고 생각하면 될 것 같다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct School{
public int student_number;
public int a;
public int b;
public int c;
public int d;
public void Get_number(int Value){
student_number = Value;
}
// 생성자
public School(int _a, int _b, int _c, int _d){
a = _a; b = _b; c = _c; d = _d;
}
}
public class Unity_lecture : MonoBehaviour
{
// class와 struct의 차이점
// struct를 사용할 경우
School jae;
// class를 사용할 경우
// School jae = new School();
// 생성과 선언을 동시에
School yong = new School(1, 2, 3, 4);
void Start()
{
jae.student_number = 5;
jae.Get_number(5);
print(jae.student_number); // 5 출력
print(yong.a);
print(yong.c);
}
}
Enum
선택지를 주고 선택지 내에 없는 변수가 들어오면 오류발생
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Item
{
Weapon,
Shield,
Potion,
}
public class Unity_lecture : MonoBehaviour
{
Item item;
void Start()
{
item = Item.Weapon;
item = Item.Shield;
print(item); //Shield출력
}
}
강의자료: 케이디 유튜브
반응형