3
I’m making a Builder and I’d like you to behave as follows:
abstract class ClassePaiBuilder
{
public ClassePaiBuilder SetAtributo(string atributo)
{
// codigo
return this;
}
public string Build()
{
string result = "";
//processo os atributos e gero uma string
return result;
}
}
class ClasseFilhaBuilder : ClassePaiBuilder
{
public ClasseFilhaBuilder SetOutroAtributo(string outroAtributo)
{
// codigo
return this;
}
}
class Program
{
public void Run()
{
string valorQualquer = new ClasseFilhaBuilder()
.SetAtributo("atributo da classe pai")
.SetOutroAtributo("atributo da classe filha")
.Build();
}
}
Is it possible? In this case, the compilation is breaking because in the SetAtributo I’m returning the instance typed as ClassePaiBuilder, who does not have the method SetOutroAtributo.
Those
return thisso sure? Or was this just for example?– Jéf Bueno
Yes @jbueno, you’re right.
– Guilherme Silva
Okay. But what’s the point of this? The code on
Run()doesn’t make sense.– Jéf Bueno
The goal can be anything, imagine that at the end I will generate a string. I edited to try to improve the example
– Guilherme Silva