时间:2021-05-20
本文实例讲述了C#组合模式。分享给大家供大家参考。具体如下:
Company.cs如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ public abstract class Company { protected string name; public Company(string name) { this.name = name; } public abstract void Add(Company c); public abstract void Remove(Company c); public abstract void Display(int depth); public abstract void LineOfDuty(); }}ConcreteCompany.cs如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;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) { children.Add(c); } public override void Remove(Company c) { children.Remove(c); } public override void Display(int depth) { Console.WriteLine(new String('-',depth)+name); foreach(Company component in children) { component.Display(depth+2); } } public override void LineOfDuty() { foreach(Company component in children) { component.LineOfDuty(); } } }}FinanceDepartment.cs如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;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) { Console.WriteLine(new String('-',depth)+name); } public override void LineOfDuty() { Console.WriteLine("{0} 财务支付管理",name); } }}HRdepartment.cs如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;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) { Console.WriteLine(new String('-',depth)+name); } public override void LineOfDuty() { Console.WriteLine("{0} 招聘培训管理",name); } }}Program.cs如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { ConcreteCompany root = new ConcreteCompany("北京总共司"); root.Add(new HRdepartment("人力部")); root.Add(new FinanceDepartment("财务部")); ConcreteCompany comp = new ConcreteCompany("上海分公司"); comp.Add(new HRdepartment("分工司人力部")); comp.Add(new FinanceDepartment("分公司财务部")); root.Add(comp); ConcreteCompany comp1 = new ConcreteCompany("南京分部"); comp1.Add(new HRdepartment("南京人力部")); comp1.Add(new FinanceDepartment("南京财务部")); comp.Add(comp1); ConcreteCompany comp2 = new ConcreteCompany("杭洲分部"); comp2.Add(new HRdepartment("杭州人事部")); comp2.Add(new FinanceDepartment("杭州财务部")); comp.Add(comp2); root.Display(1); Console.ReadKey(); } }}希望本文所述对大家的C#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C#动态创建button按钮的方法实例详解C#编程中经常需要动态创建,本文主要介绍C#动态创建button按钮的方法,涉及C#按钮属性动态设置的相关技巧,以供借
本文实例讲述了c#桥接模式(bridge结构模式)用法。分享给大家供大家参考。具体分析如下:一般来说,Bridge模式使用“对象间的组合关系”解耦了抽象和实现之
本文实例讲述了C#实现单件模式的三种常用方法。分享给大家供大家参考。具体分析如下:单件模式是一种设计模式,即保持同时只能创建一个实例,下面列出了C#实现单件模式
本文以实例形式详细讲述了C#装饰器模式的实现方法。分享给大家供大家参考。具体实现方法如下:现假设有一个公司要做产品套餐,即把不同的产品组合在一起,不同的组合对应
本文实例讲述了C#实现生成所有不重复的组合功能。分享给大家供大家参考,具体如下:给你几个字母,比如(a,b,c,d,e,f),要求生成所有不重复的组合。这里重复