suppose you have the interface IPhysicalObject, which implements the following members: (C#, you get the idea though)
Code:
public interface IPhysicalObject
{
double GetWeight();
string GetName();
Color GetColor();
};
and you then have the following classes implement it:
Code:
public class MyBox : IPhysicalObject
{
public double GetWeight() { return 15.00; } ;
public String GetName() { return "My Heavy Red Chest"; };
public Color GetColor() { return Color.Red; };
public void Lock() { /* .... */ };
public void Unlock() { /* .... */ };
};
public class MyFridge: IPhysicalObject
{
public double GetWeight() { return 100.00; } ;
public String GetName() { return "My Big Refrigerator"; };
public Color GetColor() { return Color.White; };
public bool IsFoodRotten() { /* .... */ };
};
public class MyShirt: IPhysicalObject
{
public double GetWeight() { return .5; } ;
public String GetName() { return "My Green Shirt"; };
public Color GetColor() { return Color.Green; };
public void TurnInsideOut() { /* .... */ };
};
now suppose you wanted to keep these three things in an array together, maybe for some weird game where you had objects in a room:
Code:
myShirt shirt = new myShirt();
myFridge fridge = new myFridge();
myBox box = new myBox();
ArrayList myRoom = new ArrayList();
myRoom.Add(shirt);
myRoom.Add(fridge);
myRoom.Add(box);
since all the objects in your array implement IPhysicalObject, you can do something like this:
Code:
foreach(IPhysicalObject myObj in myRoom)
{
Console.Write("This Object [" + myObj.Name + "]");
Console.WriteLine("is " + myObj.Color.ToString());
}