Difference between Abstract and Virtual Function에서 추상 및 가상 메서드의 차이점을 살펴 보겠습니다. 오버라이드 및 새 기능과 혼동 됨 C#
나는이의이
class MainClass
{
public static void Main()
{
DerivedClass _derived = new DerivedClass();
_derived.SayHello();
_derived.SayGoodbye();
Console.ReadLine();
}
}
public abstract class AbstractClass
{
public void SayHello()
{
Console.WriteLine("Hello - abstract member\n");
}
public virtual void SayGoodbye()
{
Console.WriteLine("Goodbye- abstract member \n");
}
//public abstract void SayGoodbye();
}
public class DerivedClass : AbstractClass
{
public new void SayHello()
{
Console.WriteLine("Hi There - Hiding base class member");
}
//public override void SayGoodbye()
//{
// Console.WriteLine("See you later - In derived class OVERRIDE function");
//}
public new void SayGoodbye()
{
Console.WriteLine("See you later - In derived class I'm in NEW member");
}
}
아래와 같이 샘플 코드를 살펴 보자 virtual
및 new
에 관한 의심을 가지고 내 질문 : override
및 new
는 동일한 기능을 수행하는 방법 파생 클래스에서 , 내가 SayGoodbye
라고 전화하면? 내가 그들 중에서 선택/선호하기 위해 갈 필요가있을 때? 어느 시나리오에서 내가 그들 가운데 선호해야 할 것인가?
를 사용하는 경우 http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding -in-c – Oded
질문/답변 @Oded 링크를 사용하면 질문에 완벽하게 답변 할 수 있습니다. –
위의 코드를 사용해보십시오. Console.WriteLine ((AbstractClass) _derived.SayGoodbye());'. 기본 클래스 메소드를 실행합니다. 당신이 숨겨진 대신에 무효화 되었다면 그런 일은 일어나지 않을 것입니다. –