목표

  • virtual 키워드에 대해 알아보자

virtual

  • virtual 키워드는 메서드(method), 속성(property), 인덱서(indexer), 이벤트(event) 선언을 수정하고 파생 클래스에서 재정의를 허용하는데 사용된다.

  • virtual member의 구현은 파생 클래스에서 재정의 할 수 있다.

  • virtual 키워드는 static, abstract, private, overried 한정자와 함께 사용할 수 없다.

virtual auto-implemented properties

  • 자동 구현된 가상 속성은 get/set 접근자를 구현하는 경우에만 특수 동작을 제공할 수 있다.

  • 다음 예제에서 Date를 출력하는 클래스를 예로 들어보자.

  • DefaultDate는 자동구현된 가상 속성을 가지고 있는 객체이다.

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

public class DefaultDate
{
    public virtual string Date
    {
        get; set;
    }
}
  • DerivedDate는 DefaultDate를 상속받고 속성을 재정의한 객체이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DerivedDate : DefaultDate  
{  
    private string date;

  public override string Date  
  {  
    get  
    {  
      return date;  
    }  
    set  
    {  
      date = "DerivedDate : " + value;  
    }  
  }  
}
  • BaseDate는 DefaultDate를 상속 받았지만, 자동 구현된 가상 속성을 재정의하지 않았다.
using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

public class BaseDate : DefaultDate  
{

}
  • DerivedDate는 재정의한 속성을 사용하여 값을 리턴하고, BaseDate는 DefaultDate의 속성을 사용하여 값을 리턴한다.

  • 이처럼 가상 속성을 재정의하면 재정의된 속성이 사용되고, 재정의하지 않으면 부모 클래스의 속성을 사용한다.

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

public class Timer : MonoBehaviour  
{  
  private DerivedDate derivedDate;  
  private DefaultDate defaultDate;

  // Start is called before the first frame update
  void Start()
  {
      derivedDate = new DerivedDate();
      defaultDate = new DefaultDate();

      derivedDate.Date = System.DateTime.Now.ToString();
      defaultDate.Date = System.DateTime.Now.ToString();

      Debug.Log("Derived Date " + derivedDate.Date);
      Debug.Log("Default Date " + defaultDate.Date);
  }

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

  }  
} 

virtual property

  • 자동 구현되었다는 점을 제외하면 virtual auto-implemented properties와 동일한다.

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

public class DefaultDate  
{  
  private string date;  
  public virtual string Date  
  {  
    get  
    {  
        return date;  
    }  
    set  
    {  
        date = value;  
    }  
  }  
}

virtual method

  • 위와 같은 방식으로 함수 또한 virtual로 사용 가능하다.
  • 다음 예제에서 기본 모양인 Shape는 가로와 세로 값을 가지고 넓이를 리턴한다.
using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

public class Shape  
{  
  protected double width;  
  protected double height;

  public Shape(double width, double height)
  {
      this.width = width;
      this.height = height;
  }

  public virtual double Area()
  {
      return width * height;
  }  
} 
  • 원의 넓이는 반지름 * 반지름 * PI 임으로, 아래와 같이 재정의 할 수 있다.
using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;

public class Circle : Shape  
{  
  public Circle(double r) : base(r, 0)  
  {  
  }

  public override double Area()
  {
      return Mathf.PI * Mathf.Pow(x,2);
  }  
}

+ Recent posts