Hello!
Assume we have the following classes and interfaces.
We have a class called Inventory that is associated to a class called
Product.
We have some classes that is derived from this Product class and from two
interfaces called Wearable and Rentable.
These classes has the following names Book, Soda, Shoe, Coffee, BubbleGum
and Underwear.
Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I have a
question about
Normally when we have an array we do Product[] inventory = new
Product[50]; // allocating memory for the array
But here you assign the return value from method "read_all_products();".
I can't understand the construction assigning the return value from this
read_all_products();"?
What type does this read_all_products(); actually returns?
I just can't understand how it's possible to load the array without
allocation memory by using the new operator.?
If this instead had the following construction I would understand it.
Product[] inventory = new Product[50];
read_all_products();
All this is about explaining this row construction. "Product[] inventory
= read_all_products();"
namespace Inventory
{
public class ProductHandler
{
public static void Main()
{
Product[] inventory = read_all_products(); //load inventory
foreach (Product p in inventory) //Check all products in array
{
if (p is Rentable) //It it rentable?
assign_to_rental_department(p); //Allocate it
if (p is Wearable) //Is it wearable?
{
Wearable w = (Wearable) p; //Yes, cast it so
w.PutOn(); // we can put
it on
}
}
}
}
}
//Tony