I want to implement a mini market program . there is a abstract class named (Goods) and two derived classes from it (dry and cold goods). How to add some items after that ? (eggs , milk,rice...etc)
You can't instantiate an abstract class. It's sole purpose is to act as a base class. Your Eggs, Milk, Rice class must derive from the Goods and implement the functionality, as shown below:
public abstract class Goods
{
}
public class DryGoods : Goods
{
}
public class ColdGoods : Goods
{
}
You can then have the more primitive items inheriting from Dry or Cold goods.
public class Egg : DryGoods
{
}
public class Milk : ColdGoods
{
}
Alternatively if you don't need the Milk
or Egg
as light weight classes (Or transferable objects) you could just use the derived DryGoods
or ColdGoods
types directly and have a GoodsType
property:
DryGoods egg = new DryGoods();
egg.GoodsType = DryGoods.Egg;
ColdGoods milk = new ColdGoods();
milk.GoodsType = ColdGoods.Milk;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With