프로그래밍 언어/C#
C# 클래스 상속 불가 및 함수 오버라이딩 불가 (sealed)
ShovelingLife
2022. 7. 21. 11:21
클래스에 적용된 경우 sealed 한정자는 다른 클래스가 해당 클래스에서 상속하지 못하도록 한다.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class Parent
{
}
public sealed class Derived : Parent
{
}
public class AnotherDerived : Derived
{
}
public class Test
{
public static void Main()
{
AnotherDerived anotherDerived = new AnotherDerived();
}
}
함수 같은 경우에는 가상 함수 오버라이딩 방지용으로 쓰여진다.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class Parent
{
public virtual void Print()
{
Console.WriteLine("Parent Class");
}
}
public class Derived : Parent
{
public override sealed void Print()
{
Console.WriteLine("Derived Class");
}
}
public class Another:Derived
{
public override void Print()
{
}
}
public class Test
{
public static void Main()
{
}
}