通常情况下,有固定的工作时尺寸类别(您的情况使用8
)我们用enum
类型:
public enum ProductCategory {
Food,
Clothes,
//TODO: put all the other categories here
}
加起来图标,字符串等,我们可以实现扩展方法:
public static class ProductCategoryExtensions {
// please, notice "this" for the extension method
public static string IconName(this ProductCategory value) {
switch (value) {
case ProductCategory.Food:
return "apple.jpg";
case ProductCategory.Clothes:
return "tshirt.jpg";
//TODO: add all the other categories here
default:
return "Unknown.jpg";
}
}
}
最后
public class Product {
public string Name { get; set; }
public double Price { get; set; } // decimal is a better choice
public ProductCategory Category { get; set; }
}
使用
Product test = new Product();
test.Category = ProductCategory.Clothes;
Console.Write(test.Category.IconName());
创建类别新的类/接口也,就像产品一样。 – Anil
再上课。一个类是一个类型。不过,并非所有类型都是类。 –
看看任何C#教程或任何你喜欢的书。创建一个新类是* any *面向对象类的基础,因此可以在* every * tutorial/book中解释。 – HimBromBeere