C# 如何在c中拥有一种虚拟静态成员#
我有一门课,像:C# 如何在c中拥有一种虚拟静态成员#,c#,virtual,overloading,static-members,C#,Virtual,Overloading,Static Members,我有一门课,像: public class Food { public static IList<Ingredient> ingredients public bool uses(Ingredient i) { return ingredients.Contains(i); } } 因此,我希望有一个所有蛋糕都可以共享的静态配料列表,而不是每个蛋糕实例都有一个列表,并且能够以虚拟的方式访问它 如果需要的话,我愿意将代码四处搅动,以获得类似的效果 您可以定义子
public class Food
{
public static IList<Ingredient> ingredients
public bool uses(Ingredient i)
{
return ingredients.Contains(i);
}
}
因此,我希望有一个所有蛋糕都可以共享的静态配料列表,而不是每个蛋糕实例都有一个列表,并且能够以虚拟的方式访问它
如果需要的话,我愿意将代码四处搅动,以获得类似的效果 您可以定义子类覆盖的方法
getComponents()
:
public abstract class Food
{
public static IList<Ingredient> ingredients
public bool uses(Ingredient i)
{
return GetIngredients().Contains(i);
}
protected abstract IList<Ingredient> GetIngredients();
}
public class Cake : public Food
{
public static IList<Ingredient> ingredients = new List<Ingredient>()
{
Sugar,
Eggs,
Milk
}
protected override IList<Ingredient> GetIngredients()
{
return ingredients ;
}
}
公共抽象类食品
{
公共静态IList成分
公共用途(成分一)
{
返回getComponents()。包含(i);
}
受保护的抽象IList getComponents();
}
公共类蛋糕:公共食品
{
公共静态IList成分=新列表()
{
糖
鸡蛋,
牛奶
}
受保护的覆盖IList GetComponents()
{
返回成分;
}
}
让所有这些实例都成为一个类怎么样?比如:
class Food
{
public IList<Ingredient> ingredients;
public Food(IList<Ingredient> ingredients)
{
this.ingredients = ingredients.Clone();
}
public bool uses(Ingredient i)
{
return ingredients.Contains(i);
}
}
Food Cake = new Food({ Sugar, Eggs, Milk });
类食品
{
公共卫生服务;
公共食品(IList成分)
{
this.components=components.Clone();
}
公共用途(成分一)
{
返回成分。包含(i);
}
}
食品蛋糕=新食品({糖、蛋、牛奶});
在这种情况下,食品设计办公室“更喜欢”将食品类抽象化,因为“成分”不同于食品类。因此,正如其他人所指出的那样,列出这个static
是没有意义的,因为static
应该是一个属性的修饰符,该属性不受对象的影响
我建议的解决办法如下:
public abstract class Food
{
public abstract IList<Ingredient> Ingredients
{
get;
}
}
成分不需要是静态的。您可以从Cake类派生所有类型的不同蛋糕,以便它们继承常见成分:
public class Cake : Food
{
public IList<Ingredient> ingredients = new List<Ingredient>()
{
Sugar,
Eggs,
Milk
}
}
public class DevilsFoodCake : Cake
{
public DevilsFoodCake()
{
ingredients.AddRange(new List<Ingredient>()
{
Chocolate,
SourCream
}
}
}
公共类蛋糕:食物
{
公共IList成分=新列表()
{
糖
鸡蛋,
牛奶
}
}
公共类魔鬼食品蛋糕:蛋糕
{
公共设备FoodCake()
{
配料.添加范围(新列表()
{
巧克力,
酸流
}
}
}
如果你的目标是每种类型都有一个成分集合,那么你就不可能真正做到。我认为你能做的最好的事情,尽管不是我不推荐的事情,就是负责任地将材料转移到另一个类中,就像这样:
public class IngredientStore
{
public static IngredientStore Current
{
get;
private set;
}
private Dictionary<Type, List<Ingredient>> ingredients = new Dictionary<Type, List<Ingredient>>();
static IngredientStore()
{
Current = new IngredientStore();
}
public void Register(Type type, List<Ingredient> ingredients)
{
this.ingredients[type] = ingredients;
}
public bool Uses(Type type, Ingredient ingredient)
{
// TODO: check the type is registered
return this.ingredients[type].Contains(ingredient);
}
}
public class Food
{
public bool Uses(Ingredient ingredient)
{
return IngredientStore.Current.Uses(this.GetType(), ingredient);
}
}
public class Cake : Food
{
static Cake()
{
IngredientStore.Register(typeof(Cake), new List<Ingredient>
{
Ingredient.Butter,
Ingredient.Eggs,
Ingredient.Flour,
};
}
}
public class CherryCake : Cake
{
static CherryCake()
{
IngredientStore.Register(typeof(CherryCake), new List<Ingredient>
{
Ingredient.Butter,
Ingredient.Eggs,
Ingredient.Flour,
Ingredient.Cherries,
};
}
}
公共类IngCreditStore
{
公共静态IngCreditStore当前
{
得到;
私人设置;
}
私有字典成分=新字典();
静态IngreditStore()
{
当前=新建IngredientStore();
}
公共无效登记簿(类型、成分列表)
{
这个。成分[类型]=成分;
}
公共布尔用途(类型、成分)
{
//TODO:检查类型是否已注册
返回此。配料[type]。包含(配料);
}
}
公共食品
{
公共用途(成分)
{
返回IngreditStore.Current.Uses(this.GetType(),Component);
}
}
公共类蛋糕:食物
{
静态滤饼()
{
IngCreditStore.Register(蛋糕类型),新列表
{
配料,黄油,
配料,鸡蛋,
配料,面粉,
};
}
}
公共级樱桃蛋糕:蛋糕
{
静态樱桃蛋糕()
{
IngredientStore.Register(类型)(樱桃蛋糕),新列表
{
配料,黄油,
配料,鸡蛋,
配料,面粉,
配料:樱桃,
};
}
}
@Gtevil为什么你必须使你的列表静态
?此外,在C#
中没有公共
或私有
继承。所有继承必须是公共
@Mr.DDD我希望列表是静态的,以节省内存并避免不必要的列表初始化,我只需要在应用程序上做一次应用程序启动,不是每个实例一次。是的-我不需要公共继承,五一劳动日的工作太多了!有什么原因使Cake
本身就是一个类吗?那不应该只是Food
的一个实例吗?它不会编译。你不能像那样使用数组初始值设定项。还有集合初始值设定项需要集合的类型。@svick:所以请使用普通的列表初始值设定项……我很久没有使用C#了。我想使用static的想法是,对于同一类的所有实例,成分始终保持不变。
public class Cake : Food
{
public override IList<Ingredient> Ingredients
{
get {
return new IList<Ingredient>()
{ Sugar, Eggs, Milk };
}
}
}
public class Cake : Food
{
public IList<Ingredient> ingredients = new List<Ingredient>()
{
Sugar,
Eggs,
Milk
}
}
public class DevilsFoodCake : Cake
{
public DevilsFoodCake()
{
ingredients.AddRange(new List<Ingredient>()
{
Chocolate,
SourCream
}
}
}
public class IngredientStore
{
public static IngredientStore Current
{
get;
private set;
}
private Dictionary<Type, List<Ingredient>> ingredients = new Dictionary<Type, List<Ingredient>>();
static IngredientStore()
{
Current = new IngredientStore();
}
public void Register(Type type, List<Ingredient> ingredients)
{
this.ingredients[type] = ingredients;
}
public bool Uses(Type type, Ingredient ingredient)
{
// TODO: check the type is registered
return this.ingredients[type].Contains(ingredient);
}
}
public class Food
{
public bool Uses(Ingredient ingredient)
{
return IngredientStore.Current.Uses(this.GetType(), ingredient);
}
}
public class Cake : Food
{
static Cake()
{
IngredientStore.Register(typeof(Cake), new List<Ingredient>
{
Ingredient.Butter,
Ingredient.Eggs,
Ingredient.Flour,
};
}
}
public class CherryCake : Cake
{
static CherryCake()
{
IngredientStore.Register(typeof(CherryCake), new List<Ingredient>
{
Ingredient.Butter,
Ingredient.Eggs,
Ingredient.Flour,
Ingredient.Cherries,
};
}
}