본문 바로가기
유니티/개념 정리

This

by fore4022 2024. 9. 9.

this는 자신의 클래스 인스턴스를 가리킨다.

this의 사용

  1. 비슷한 이름으로 가려진 멤버를 호출할 수 있다.
    using UnityEngine;
    public class Test : MonoBehaviour
    {
        private string testString = "Test String";
        private float testFloat = 0.5f;
        private int testInt = 10;
    
        private void Start()
        {
            TestLog("Test", 0, 0);
        }
        private void TestLog(string testString, float testFloat, int testInt)
        {
            Debug.Log(testString);
            Debug.Log(this.testString);
    
            Debug.Log(testFloat);
            Debug.Log(this.testFloat);
    
            Debug.Log(testInt);
            Debug.Log(this.testInt);
        }
    }//실행 결과 : "Test", "Test String", 0f, 0.5f, 0, 10이 차례대로 출력되는 것을 확인할 수 있다.
    ​
  2. this로 가져온 instance를 메서드의 매개변수로 넘겨줄 수 있다.
    using UnityEngine;
    public class Test : MonoBehaviour
    {
        private int testInt = 10;
    
        private void Start()
        {
            TestLog(this);
        }
        private void TestLog(MonoBehaviour mono)
        {
            Debug.Log(mono.GetType());
        }
    }//실행 결과 : this로 넘겨준 인스턴스의 타입 이름인 Test가 출력된다.
    ​
  3. 인덱서를 정의할 수 있다.
     인덱서(Indexer)
 

인덱서(Indexer)

배열 형식으로 내부 요소에 접근이 가능하다.인덱스를 통하여 객체와 인스턴스 변수에 접근할 수 있다.배열처럼 값에 접근할 수 있다.장점배열을 통하여서 값을 빠르게 가져올 수 있다.값을 명

fore4022.tistory.com

'유니티 > 개념 정리' 카테고리의 다른 글

상속  (0) 2024.09.23
StopWatch와 Time.deltaTime  (0) 2024.09.12
IInputActionCollection  (0) 2024.09.02
Task  (0) 2024.08.23
형식 테스트  (0) 2024.08.16