我有一个简单的问题。有一个基类 Product。以及手镯、耳环和戒指等派生类。但是环类有一个额外的属性。
我将如何达到该 size 属性并在下面的代码中的方法中使用它。
public class Product
{
public int id;
public string title;
}
public class Bracelet : Product
{
}
public class Earring : Product
{
}
public class Ring : Product
{
public int size;
}
Product product;
if(category = 1) // this is a Bracelet
{
product = new Bracelet();
}
else if(category = 2) // this is a Earring
{
product = new Earring();
}
else if(category = 3) // Wola, this is a ring
{
product = new Ring();
product.size = 4; // I cant reach size.. I need to assign size of the ring to decrease stock correctly.
}
product.decreaseStock();