Containment represents a HAS_A relationship between the whole and a
part.
So a car IS_A motorized vehicle, but HAS_A radio. The two relationships
can
be expressed in code thusly:
class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
Radio r= new Radio();
}
An instance of Car contains an instance of a Radio so that the lifetimes
of the
radio and car are intertwined. This is "containment by ownership" making
Car
an example of a composite class.
Containment can also be accomplished using references to external
objects.
"Containment by reference" can be expressed in code thusly:
class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
private Radio r;
public Car(Radio r) {
...
this.r= r;
}
}
You can then create an instance of Car like this:
class Class1
{
[STAThread]
static void Main(string[] args)
{
Radio r= new Radio();
Car c= new Car(r);
}
}
Regards,
Jeff
*** Sent via Developersdex
http://www.developersdex.com ***