Most Simple Aggregation

  • Thread starter Thread starter Dave Johnson
  • Start date Start date
D

Dave Johnson

greetings,



can any one show sample example of aggregation in any .net code vb.net
or C#



i guess it shouldnt be that hard but i searched a lot without finding
anything that explains the issue in code. thanks for your reply


Sharing makes All the Difference
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top