Class can contain class : Class Definition : Class C# Examples


C# Examples » Class » Class Definition »

 

Class can contain class









    
using  System;

public  class  Car  
{
    private  int  maxSpeed;

    private  Radio  theMusicBox  =  new  Radio();

    public  Car()
    {
        maxSpeed  =  100;
    }

    public  void  SwitchRadio(bool  state)
    {
        theMusicBox.TurnOn(state);
    }
}

public  class  Radio
{
    public  void  TurnOn(bool  on)
    {
        if(on)
            Console.WriteLine("ON...");
        else
            Console.WriteLine("OFF...");
    }
}

public  class  MainClass
{
    public  static  void  Main(string[]  args)
    {        
        //  Make  a  car.
        Car  c1  =  new  Car();
        c1.SwitchRadio(true);

        c1.SwitchRadio(false);
    }
}
    
   
  
   



Output

ON...
OFF...


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Class Definition