{"meta":{"title":"简化复杂的继承层次结构","intro":"Copilot Chat 可帮助你重构代码以避免具有多层继承的类。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot 指南"},{"href":"/zh/copilot/tutorials/copilot-cookbook/refactor-code","title":"重构代码"},{"href":"/zh/copilot/tutorials/copilot-cookbook/refactor-code/simplify-inheritance-hierarchies","title":"简化继承层次结构"}],"documentType":"article"},"body":"# 简化复杂的继承层次结构\n\nCopilot Chat 可帮助你重构代码以避免具有多层继承的类。\n\n继承层次结构过深或继承使用不当，会导致代码难以维护，难以遵循类之间的关系，或在不造成破坏的情况下扩展类。\n\nCopilot Chat 可以建议将基于继承的设计重构为更灵活的组合式设计（优先使用组合而非继承）。 它还建议应用策略或修饰器等模式，在不增加继承结构复杂性的情况下，使系统更具可扩展性。\n\n## 示例方案\n\n下面的 C# 代码具有很深的继承层次结构，每个类都建立在前一个类的基础上。 这样就形成了一个很长的继承链，导致很难知道每个类都有哪些属性。\n\n此类多级继承层次结构可能难以理解、修改和扩展。\n\n```csharp id=employee-class\npublic class Employee\n{\n    public string Name { get; set; }\n    public int EmployeeID { get; set; }\n}\n\npublic class Manager : Employee\n{\n    public int NumberOfTeams { get; set; }\n}\n\npublic class SeniorManager : Manager\n{\n    public string Region { get; set; }\n}\n\npublic class Director : SeniorManager\n{\n    public int NumberOfDepartments { get; set; }\n}\n\npublic class VicePresident : Director\n{\n    public string Division { get; set; }\n}\n\npublic class President : VicePresident\n{\n    public string Company { get; set; }\n}\n```\n\n## 示例提示\n\n在这种情况下，你可能会决定使用一个父类 `Employee`，其他类应继承其属性，但通过为每个类单独定义所有其他属性来避免任何其他继承。\n\n```copilot copy prompt ref=employee-class\nRefactor this code so that classes only inherit the properties of the Employee class. All of the other properties that each class currently inherits should instead be declared independently in each class definition.\n```\n\n## 示例响应\n\n> \\[!NOTE] 以下响应是示例。 Copilot Chat 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nCopilot 建议以下代码：\n\n```csharp\npublic class Employee\n{\n    public string Name { get; set; }\n    public int EmployeeID { get; set; }\n}\n\npublic class Manager : Employee\n{\n    public int NumberOfTeams { get; set; }\n}\n\npublic class SeniorManager : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n}\n\npublic class Director : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n}\n\npublic class VicePresident : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n    public string Division { get; set; }\n}\n\npublic class President : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n    public string Division { get; set; }\n    public string Company { get; set; }\n}\n```\n\n## 延伸阅读\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/concepts/prompting/prompt-engineering)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/get-started/best-practices)"}