public class Part
{
public class Builder
{
public string Name { get; set; }
public int Type { get; set; }
public Part Build()
{
return new Part(Name, Type);
}
}
protected Part(string name, int type)
{
...
}
}
주 보호 생성자를 - 내가 빌더를 사용하여 얼마나 좋아 부품을 얻으려면.
Part p = new Part.Builder() { Name = "one", Type = 1 }.Build();
으로 전화하면됩니다.
public class SpecialPart : Part
{
protected SpecialPart(string name, int type) : base(name, type) { }
}
그리고 빌더에 약간의 변화 : 내가 뭘하고 싶은 것은 (예를 들어) 유형에 따라 일부의 특별한 종류까지 제공하기 위해이 빌더를 사용하는 것입니다
public Part Build()
{
if (Type == _some_number_)
return new SpecialPart(Name, Type);
return new Part(Name, Type);
}
을하지만, 이 작동하지 않습니다 - Part.Builder는 SpecialPart의 보호 된 생성자를 볼 수 없습니다. 빌더가 Part의 자손과 함께 작업하고 동일한 필연적 인 빌더 개념을 갖도록하려면 어떻게해야합니까?
Builder가 클래스이며 보호 된 메소드가 아닌 이유가 있습니까? – Robert
@ 로버트 : protected = public? –
예, 공개합니다. 나는 생성자에 대해 생각하고 있었다. – Robert