为了使我的代码更有组织性,我决定使用流畅接口;然而,在阅读可用的教程时,我发现许多实现这种流畅性的方法,其中我发现一个主题,它说要创建流畅的
以下是我如何实现流畅API的代码:
然而,我如何更有效地使用接口来创建流畅的API呢?
Interface
我们应该利用Interfaces
,但他没有提供任何好的细节来实现它。以下是我如何实现流畅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);
然而,我如何更有效地使用接口来创建流畅的API呢?