SoFunction
Updated on 2025-03-07

Detailed explanation of C# combination mode example

This article describes the C# combination mode. Share it for your reference. The details are as follows:

as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  public abstract class Company
  {
    protected string name;
    public Company(string name) 
    {
       = name;
    }
    public abstract void Add(Company c);
    public abstract void Remove(Company c);
    public abstract void Display(int depth);
    public abstract void LineOfDuty();
  }
}

as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  public class ConcreteCompany:Company
  {
    private List<Company> children = new List<Company>();
    public ConcreteCompany(string name) 
      :base(name)
    {}
    public override void Add(Company c)
    {
      (c);
    }
    public override void Remove(Company c)
    {
      (c);
    }
    public override void Display(int depth)
    {
      (new String('-',depth)+name);
      foreach(Company component in children)
      {
        (depth+2);
      }
    }
    public override void LineOfDuty()
    {
      foreach(Company component in children)
      {
        ();
      }
    }
  }
}

as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  public class FinanceDepartment:Company
  {
    public FinanceDepartment(string name) : base(name) { }
    public override void Add(Company c)
    {
    }
    public override void Remove(Company c)
    {
      
    }
    public override void Display(int depth)
    {
      (new String('-',depth)+name);
    }
    public override void LineOfDuty()
    {
      ("{0} Financial payment management",name);
    }
  }
}

as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  public class HRdepartment:Company
  {
    public HRdepartment(string name)
      :base(name)
    { }
    public override void Add(Company c)
    {
    }
    public override void Remove(Company c)
    {
    }
    public override void Display(int depth)
    {
      (new String('-',depth)+name);
    }
    public override void LineOfDuty()
    {
      ("{0} Recruitment and Training Management",name);
    }
  }
}

as follows:

using System;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ConcreteCompany root = new ConcreteCompany("Beijing General Department");
      (new HRdepartment("Ministry of Human Resources"));
      (new FinanceDepartment("Treasury Department"));
      ConcreteCompany comp = new ConcreteCompany("Shanghai Branch");
      (new HRdepartment("Division of Labor Department Human Resources Department"));
      (new FinanceDepartment("Branch Finance Department"));
      (comp);
      ConcreteCompany comp1 = new ConcreteCompany("Nanjing Branch");
      (new HRdepartment("Nanjing Ministry of Human Resources"));
      (new FinanceDepartment("Nanjing Finance Department"));
      (comp1);
      ConcreteCompany comp2 = new ConcreteCompany("Hangzhou Branch");
      (new HRdepartment("Hangzhou Human Resources Department"));
      (new FinanceDepartment("Hangzhou Finance Department"));
      (comp2);
      (1);
      ();
    }
  }
}

I hope this article will be helpful to everyone's C# programming.