Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate abstract class c#? [closed]

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)

like image 436
Saja Avatar asked Apr 30 '16 11:04

Saja


1 Answers

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;
like image 141
Darren Avatar answered Nov 08 '22 13:11

Darren