과부하 해결에 대한 질문이 있습니다. 유형의 매개 변수 person
을 허용하는 제한된 일반 메서드 Greet
이 있고 Person
에서 파생되어야한다고 가정합니다. 이 Greet
메서드는 person
을 매개 변수로 전달하는 다른 클래스의 일부 비 제너릭 메서드를 호출합니다. 비 제네릭 메서드에는 기본 클래스 및 파생 클래스에 대한 여러 가지 오버로드가 있습니다. 기본 클래스의 오버로드가 항상 호출되는 이유는 무엇입니까?이상한 메서드 오버로드 해결 제한된 일반 인수 : 기본 클래스 arg가 항상 호출 된 오버로드
예 :
public static class Test
{
public static void Greet()
{
new SomeClass().Greet(new Person()); // Hi, I am a Person
new SomeClass().Greet(new Manager()); // Hi, I am a Person -- Why?
}
}
public class SomeClass
{
public void Greet<T>(T person) where T : Person
{
new Greeter().SayHi(person);
}
}
public class Person { /* some props and methods */ }
public class Manager : Person { /* some props and methods */ }
public class Greeter
{
public SayHi(Person person) { Console.WriteLine("Hi, I am a person"); }
public SayHi(Manager person) { Console.WriteLine("Hi, I am a manager"); }
}
그러나, 사람을 받아들이에만 SayHi 항상라고합니다. 왜?
외부에서 '동적'을 사용하면 과부하 해결은 * 런타임 *이 아닌 컴파일 타임 *에서 수행됩니다. 호출을 컴파일하려면 하나의 메소드를 선택해야합니다. –