I\'m new to interfaces and abstract classes. I want to create a couple of interfaces to define core methods and variables for the objects for a shopping cart system. Then I want
There are 2 ways to accomplish this. You can either use generics or explicitly implement interface members.
Generics
public interface ICart where TCartItem : ICartItem
{
...
List CartItems { get; set; }
}
public interface ICartItem
{
int ProductId { get; set; }
int Quantity { get; set; }
}
public abstract class Cart : ICart
{
private List _cartItems = new List();
public List CartItems
{
get
{
return _cartItems;
}
}
}
public abstract class CartItem : ICartItem
{
...
}
Explicitly implemented members
public interface ICart
{
...
List CartItems { get; set; }
}
public interface ICartItem
{
int ProductId { get; set; }
int Quantity { get; set; }
}
public abstract class Cart : ICart
{
private List _cartItems = new List();
List ICart.CartItems
{
get
{
return CartItems;
}
}
public List CartItems
{
get
{
return _cartItems;
}
}
}
public abstract class CartItem : ICartItem
{
...
}