假设我有一组从模板生成文档的类。例如……
class CustomerInvoice
{
public satic string TemplatePath
{
get { return @"C:\Templates\InvoiceTemplate.doc"; }
}
public static DocumentType DocumentType
{
get { return DocumentType.WordDocument; }
}
public static void Create(Customer customer, int orderNumber)
{
//...
}
}
所有这些类共享相同的方法名称,但不一定是方法签名。
例如,我可能有
CustomerInvoice.Create(Customer customer, int orderNumber);
DespatchNote.Create(Customer customer, int orderNumber, Warehouse warehouse);
PackingLabel.Create(int orderNumber);
... 或其他(努力想出合理的例子)。
OO中是否有一种机制可以指定一组类以这种方式命名的方法?我真的在想有一种方法可以在一组相似的对象中强制执行一致的实现和命名,因此它们对消费者来说更直观。像这样的案例会被认为是对任何此类技术的有效/有值(value)的使用吗?
最佳答案
不,没有用于此的结构。静态方法特别是 无法强制执行任何通用性。如果是一个接口(interface),你可以有类似 Create(object)
或 Create(T)
的东西(对于在通用接口(interface)上定义的一些 T)并采用单个参数来表示德州。
关于c# - 如何以比 C# 中的接口(interface)更宽松的方式定义模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4561796/