Suppose you have a class relation like this:
class Banana : Fruit { }
If there are some public global values already assigned to fruit, is there a way to have a new banana class inherit these existing values?
For example:
class Fruit
{
public int price;
public string origins;
}
class banana : fruit
{
public string peelDensity;
public Fruit (peelDensity p, int pr, string o)
{
peelDensity = p;
price = pr;
o = origins;
}
}
Say an existing fruit instance already has its price
, origins
and etc assigned. Suppose that is actually common fruit data that applies to a particular banana. How would the banana inherit these values?
Is there a better way to do this than needing to supply all the existing values in the constructor?
banana b = new banana(peel, price, origins);
Is there a way to do something like this for a copy constructor:
public Banana(Fruit fruit){ this = fruit; }
The above is just some pseudocode. In my project, my Fruit class already has over 20 values assigned and the Banana class should inherit all of that, but I would like to figure out if there is a better way to write a more elegant constructor class.