C# 구조체와 클래스 설명 2

m._.jooong·2023년 3월 8일
0

Unity C#

목록 보기
14/22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 구조체 (struct)
// 같은 목적을 갖는 여러 데이터형 변수들과 메서드들의 집합
// 구조체 객체는 Value Type 이다.
// 구조체의 주 용도 : 상대적으로 간략한 데이터 값을 저장하는데 사용된다.
// 구조체는 상속 될 수 없다.

// 클래스 (class)
// 같은 목적을 갖는 여러 데이터형 변수들과 메서드들의 집합
// 클래스 객체는 Reference Type 이다.
// 클래스의 주 용도 : 소프트웨어를 부품화(객체지향 프로그래밍)하기 위한 도구로 사용된다.

// Value Type의 변수 : int, float, double, ..., struct(구조체) 객체
// Reference Type의 변수 : 배열, class 객체

// 구조체(struct)
public struct Student  //<-- 구조체 설계
{
    public string m_Name;   //<-- 맴버변수
    public int m_Kor;       //C#에서는 구조체에서 public 속성을 생략하면
    public int m_Eng;       //디폴트 속성은 private 속성이 된다.
    public int m_Math;
    public int m_Total;
    public float m_Avg;

    public void PrintInfo()  //<-- 맴버 메서드 정의
    {
        string str = string.Format("{0} : 국어({1}) 영어({2}) 수학({3}) 총점({4}) 평균({5:#.##})",
                        m_Name, m_Kor, m_Eng, m_Math, m_Total, m_Avg);
        Debug.Log(str);
    }
}

// 클래스 (class)
public class Item   //<-- 클래스 설계
{
    public string m_Name; //아이템 이름  //<-- 맴버 변수
    public int m_Level;   //아이템 레벨
    public int m_Star;    //아이템 성급
    public int m_Price;   //아이템 가격
    float m_AttackRate = 1.0f; //아이템의 공격 상승률 
    //C#에서는 접근수식자를 생략하면 디폴트값은 private 속성이다.
    //protected : 외부에서 접근할 수 없고, 자신과 상속관계의 자식클래스까지는 접근을 허용하는 속성

    public void PrintInfo()  //<-- 맴버 메서드
    {
        string str = string.Format("{0} : 레벨({1}) 성급({2}) 가격({3}) 공격상승률({4:#.##})",
                        m_Name, m_Level, m_Star, m_Price, m_AttackRate);
        Debug.Log(str);
    }

    public void SetAttRate(float val)
    {
        if (val < 0.0f || 50.0f < val)
            return;

        m_AttackRate = val;
    }
}



public class Test_1 : MonoBehaviour
{
    public void Value(int aa) 
    {
        aa = 1000;
    }

    public void Reference(int[] aa)
    {
        aa[0] = 1000;
    }

    // 구조체를 매개변수로 받는 함수 (Value Type)
    public void StructMethod(Student a_St)
    {
        a_St.m_Name = "벨류구조체";
        a_St.m_Kor = 111;
        a_St.m_Eng = 111;
        a_St.m_Math = 111;
    }

    // 클래스를 매개변수로 받는 함수 (Reference Type)
    public void ItemMethod(Item a_It)
    {
        a_It.m_Name = "레퍼런스 클래스";
        a_It.m_Level = 111;
        a_It.m_Star = 111;
        a_It.m_Price = 111;
    }

    // Start is called before the first frame update
    void Start()
    {
        int xx = 0;
        Value(xx);
        Debug.Log("xx : " + xx);

        int[] yy = { 0 };
        Reference(yy);
        Debug.Log("yy[0] : " + yy[0]);

        // 구조체 (Value Type)
        Student aaa = new Student();
        aaa.m_Name = "드래곤";
        aaa.m_Kor = 0;
        aaa.m_Eng = 0;
        aaa.m_Math = 0;
        StructMethod(aaa);
        aaa.PrintInfo();

        // 클래스 (Reference Type)
        Item bbb = new Item();
        bbb.m_Name = "엘프의 반지";
        bbb.m_Level = 0;
        bbb.m_Star = 0;
        bbb.m_Price = 0;
        ItemMethod(bbb);
        bbb.PrintInfo();
    }

    // Update is called once per frame
    void Update()
    {

    }
}

0개의 댓글