필자 추상 팩토리 패턴 : http://www.dofactory.com/Patterns/PatternAbstract.aspxC# 구현 추상 팩토리 패턴 내 프로젝트 중 하나에
코드 :
public class QuestaoResposta : QuestaoBaseResposta, IQuestao,IQuestionario
{
public int IDQuestaoResposta { get; set; }
}
public class QuestaoFactory : QuestoesFactory
{
public override QuestaoBaseResposta CreateQuestao()
{
return new QuestaoResposta();
}
}
public abstract class QuestoesFactory
{
public abstract QuestaoBaseResposta CreateQuestao();
}
public class QuestaoBaseResposta : IQuestao, IMarcas, IQuestionario
{
// Constructor where i want to create a concrete instance
// of any class that inherits QuestaoBaseResposta using QuestoesFactory
// abstract class, and assign it to current instance of
// QuestaoBaseResposta class
public QuestaoBaseResposta(QuestoesFactory qf)
{
this = qf.CreateQuestao();
}
}
문제는 내가 "이"키워드를 사용하여 현재 클래스에 값을 assing 캔트 것입니다.
예 :
QuestaoBaseResposta qs = new QuestaoBaseResposta(new QuestaoFactory());
// Here i want the qs intance to be type of QuestaoResposta
// since im passing QuestaoFactory as argument,without cast anything.
qs.IDQuestaoResposta = 0;
당신이 캐스팅없이, 상속 형 (QuestaoResposta)에 QuestaoBaseResposta 클래스를 캐스팅 어떤 제안?
, 당신은 this''에 할당 할 수 없습니다를 – Will