为了使我的代码更有条理,我决定使用流畅的接口;然而,通过阅读可用的教程,我发现了很多实现这种流畅性的方法,其中我发现了一个主题,说要创建 FluentInterface
我们应该使用Interfaces
,但他没有提供任何好的细节来实现它。
这是我如何实现 Fluent API
代码
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public static Person CreateNew()
{
return new Person();
}
public Person WithName(string name)
{
Name = name;
return this;
}
public Person WithAge(int age)
{
Age = age;
return this;
}
}
使用代码
Person person = Person.CreateNew().WithName("John").WithAge(21);
但是,我怎样才能让接口以更有效的方式创建 Fluent API?