public class Cat
{
    // 속성 (데이터)
    public string Name;
    public string Color;   

    // 기능 (메서드)
    public void Meow()
    {
        Debug.Log(Name + ": 야옹");
     }
}

 클래스는 속성(멤버 변수)과 기능(멤버 함수)으로 나뉩니다.

 

Cat kitty = new Cat();
kitty.Name = "키티";
kitty.Color = "하얀색";
print(kitty.Name + " : " + kitty.Color);

Cat() 생성자를 이용해 객체를 생성합니다. new 키워드는 생성자를 호출하고 객체를 생성하는 데 사용합니다. 이렇게 생성된 객체는 힙 영역에 생성이 되고 kitty는 그 객체를 가리키게 됩니다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cat
{
    // 속성 (데이터)
    public string Name;
    public string Color;   

    // 기능 (메서드)
    public void Meow()
    {
        Debug.Log(Name + ": 야옹");
     }
}

public class HelloWorld : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        Cat kitty = new Cat();
        kitty.Name = "키티";
        kitty.Color = "하얀색";
        print(kitty.Name + " : " + kitty.Color);

        Cat nero = new Cat();
        nero.Name = "네로";
        nero.Color = "검은색";
        print(nero.Name + " : " + nero.Color);

        nero.Meow();
    }
}

출력 결과 화면

Posted by 소블리애
,