override(C# 참조)

override 한정자는 상속된 메서드, 속성, 인덱서 또는 이벤트의 추상 또는 가상 구현을 확장하거나 수정하는 데 필요합니다.

예제

이 예제에서 Area는 추상 ShapesClass에서 상속되므로 Square 클래스는 Area의 재정의된 구현을 제공해야 합니다.


abstract class ShapesClass
{
    abstract public int Area();
}
class Square : ShapesClass
{
    int side = 0;

    public Square(int n)
    {
        side = n;
    }
    // Area method is required to avoid
    // a compile-time error.
    public override int Area()
    {
        return side * side;
    }

    static void Main() 
    {
        Square sq = new Square(12);
        Console.WriteLine("Area of the square = {0}", sq.Area());
    }

    interface I
    {
        void M();
    }
    abstract class C : I
    {
        public abstract void M();
    }

}
// Output: Area of the square = 144

override 메서드는 기본 클래스에서 상속된 멤버를 새로 구현합니다.override 선언에 의해 재정의된 메서드를 재정의된 기본 메서드라고 합니다.재정의된 기본 메서드의 시그니처는 override 메서드의 시그니처와 같아야 합니다.상속에 대한 자세한 내용은 상속(C# 프로그래밍 가이드)을 참조하십시오.

비 가상 메서드 또는 정적 메서드는 재정의할 수 없습니다.재정의된 기본 메서드는 virtual, abstract 또는 override 메서드이어야 합니다.

override 선언을 사용하여 virtual 메서드의 액세스 가능성을 변경할 수 없습니다.override 메서드와 virtual 메서드의 액세스 수준 한정자는 모두 동일해야 합니다.

new, static 또는 virtual 한정자를 사용하여 override 메서드를 한정할 수 없습니다.

속성 선언을 재정의할 때는 상속된 속성과 동일한 액세스 한정자, 형식 및 이름을 정확히 지정해야 하며 재정의된 속성은 virtual, abstract 또는 override여야 합니다.

override 키워드를 사용하는 방법에 대한 자세한 내용은 Override 및 New 키워드를 사용하여 버전 관리(C# 프로그래밍 가이드)Override 및 New 키워드를 사용해야 하는 경우를 참조하십시오.

이 예제에서는 Employee라는 기본 클래스와 SalesEmployee라는 파생 클래스를 정의합니다.SalesEmployee 클래스는 추가 속성 salesbonus를 포함하며 이를 고려하여 CalculatePay 메서드를 재정의합니다.

class TestOverride
{
    public class Employee
    {
        public string name;

        // Basepay is defined as protected, so that it may be 
        // accessed only by this class and derrived classes.
        protected decimal basepay;

        // Constructor to set the name and basepay values.
        public Employee(string name, decimal basepay)
        {
            this.name = name;
            this.basepay = basepay;
        }

        // Declared virtual so it can be overridden.
        public virtual decimal CalculatePay()
        {
            return basepay;
        }
    }

    // Derive a new class from Employee.
    public class SalesEmployee : Employee
    {
        // New field that will affect the base pay.
        private decimal salesbonus;

        // The constructor calls the base-class version, and
        // initializes the salesbonus field.
        public SalesEmployee(string name, decimal basepay, 
                  decimal salesbonus) : base(name, basepay)
        {
            this.salesbonus = salesbonus;
        }

        // Override the CalculatePay method 
        // to take bonus into account.
        public override decimal CalculatePay()
        {
            return basepay + salesbonus;
        }
    }

    static void Main()
    {
        // Create some new employees.
        SalesEmployee employee1 = new SalesEmployee("Alice", 
                      1000, 500);
        Employee employee2 = new Employee("Bob", 1200);

        Console.WriteLine("Employee4 " + employee1.name + 
                  " earned: " + employee1.CalculatePay());
        Console.WriteLine("Employee4 " + employee2.name + 
                  " earned: " + employee2.CalculatePay());
    }
}
/*
    Output:
    Employee4 Alice earned: 1500
    Employee4 Bob earned: 1200
*/

C# 언어 사양

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

참고 항목

참조

상속(C# 프로그래밍 가이드)

C# 키워드

한정자(C# 참조)

abstract(C# 참조)

virtual(C# 참조)

new(C# 참조)

다형성(C# 프로그래밍 가이드)

개념

C# 프로그래밍 가이드

기타 리소스

C# 참조