virtual(C# 참조)

virtual 키워드는 메서드, 속성, 인덱서 또는 이벤트 선언을 수정하고 파생 클래스에서 재정의하도록 허용하는 데 사용됩니다. 예를 들어 이 메서드는 이를 상속하는 모든 클래스에서 재정의할 수 있습니다.

public virtual double Area()
{
    return x * y;
}

가상 멤버의 구현은 파생 클래스의 재정의 멤버로 변경할 수 있습니다. virtual 키워드 사용 방법에 대한 자세한 내용은 Override 및 New 키워드를 사용하여 버전 관리Override 및 New 키워드를 사용해야 하는 경우를 참조하세요.

설명

가상 메서드가 호출되면 재정의 멤버에 대해 개체의 런타임 형식이 확인됩니다. 파생 클래스가 멤버를 재정의하지 않은 경우 가장 많이 파생된 클래스의 재정의 멤버(원래 멤버일 수 있음)가 호출됩니다.

기본적으로 메서드는 가상이 아닙니다. 가상이 아닌 메서드는 재정의할 수 없습니다.

virtual 한정자는 static, abstract, private 또는 override 한정자와 사용할 수 없습니다. 다음 예제에서는 가상 속성을 보여 줍니다.

class MyBaseClass
{
    // virtual auto-implemented property. Overrides can only
    // provide specialized behavior if they implement get and set accessors.
    public virtual string Name { get; set; }

    // ordinary virtual property with backing field
    private int _num;
    public virtual int Number
    {
        get { return _num; }
        set { _num = value; }
    }
}

class MyDerivedClass : MyBaseClass
{
    private string _name;

    // Override auto-implemented property with ordinary property
    // to provide specialized accessor behavior.
    public override string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                _name = value;
            }
            else
            {
                _name = "Unknown";
            }
        }
    }
}

선언과 호출 구문에서의 차이점을 제외하면 가상 속성은 가상 메서드처럼 작동합니다.

  • 정적 속성에서 virtual 한정자를 사용하는 것은 오류입니다.

  • override 한정자를 사용하는 속성 선언을 포함함으로써 상속된 가상 속성을 파생 클래스에서 재정의할 수 있습니다.

예시

이 예제에서 Shape 클래스는 두 개의 좌표 x, yArea() 가상 메서드를 포함합니다. Circle, CylinderSphere와 같은 서로 다른 도형의 클래스는 Shape 클래스를 상속하며, 각 모양에 대해 노출 영역이 계산됩니다. 각 파생 클래스에는 Area()의 자체 재정의 구현이 있습니다.

다음 선언에 나와 있는 것처럼 상속된 클래스 Circle, CylinderSphere는 모두 기본 클래스를 초기화하는 생성자를 사용합니다.

public Cylinder(double r, double h): base(r, h) {}

다음 프로그램은 메서드와 연결된 개체에 따라 Area() 메서드의 적절한 구현을 호출하여 각 그림의 해당 영역을 계산하고 표시합니다.

class TestClass
{
    public class Shape
    {
        public const double PI = Math.PI;
        protected double _x, _y;

        public Shape()
        {
        }

        public Shape(double x, double y)
        {
            _x = x;
            _y = y;
        }

        public virtual double Area()
        {
            return _x * _y;
        }
    }

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

        public override double Area()
        {
            return PI * _x * _x;
        }
    }

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

        public override double Area()
        {
            return 4 * PI * _x * _x;
        }
    }

    public class Cylinder : Shape
    {
        public Cylinder(double r, double h) : base(r, h)
        {
        }

        public override double Area()
        {
            return 2 * PI * _x * _x + 2 * PI * _x * _y;
        }
    }

    static void Main()
    {
        double r = 3.0, h = 5.0;
        Shape c = new Circle(r);
        Shape s = new Sphere(r);
        Shape l = new Cylinder(r, h);
        // Display results.
        Console.WriteLine("Area of Circle   = {0:F2}", c.Area());
        Console.WriteLine("Area of Sphere   = {0:F2}", s.Area());
        Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
    }
}
/*
Output:
Area of Circle   = 28.27
Area of Sphere   = 113.10
Area of Cylinder = 150.80
*/

C# 언어 사양

자세한 내용은 C# 언어 사양을 참조하세요. 언어 사양은 C# 구문 및 사용법에 대 한 신뢰할 수 있는 소스 됩니다.

참고 항목