36、编程知识与实践指南

编程知识与实践指南

1. 知识点解答

1.1 部分问题答案汇总

编号 答案 解释
8.09 B -
8.10 C -
8.11 False 泛型可用于类、方法和集合
8.12 False -
8.13 False -
8.14 True -
8.15 B -
8.16 False 若在 switch 语句中未声明默认 case,且无其他 case 匹配,则 switch 语句内不执行任何操作
9.1 B -
9.2 A -
9.3 False 可使用 [DataRow] 方法属性添加任意数量的数据点
9.4 B -
9.5 False -
9.6 False 抽象类可包含抽象方法和常规方法
9.7 False -
9.8 True -
10.1 B -
10.2 False 使用 ORM 时,存储库层通常通过数据库访问层与数据库交互
10.3 True -
10.4 True -
10.5 A -
10.6 C 处理注释掉的代码时要谨慎,多数情况下可删除
10.7 D -
10.8 A -
10.9 True -
10.10 C -
10.11 True 控制器与存储库之间存在一定耦合,但比直接调用更松散
10.12 False 这是存根的功能
10.13 False InternalsVisibleTo 可将程序集的内部成员暴露给其他程序集
10.14 C -
10.15 True -
10.16 B -
10.17 A [MethodImpl(MethodImplOptions.NoInlining)] 只能应用于方法
11.1 False -
11.2 C -
11.3 A 仅服务允许调用存储库,存储库不应调用其他存储库
11.4 True -
11.5 B -
11.6 False 丢弃运算符仍可能导致内存分配
11.7 A 因为 ItemSoldOutException 可作为 Exception 类型,所以进入第一个 catch 块
11.8 A -
12.1 True -
12.2 False 可能需要实现 InventoryService
12.3 B -
12.4 A -
12.5 Yes Dragonfruit 类可设置 IsFruit 属性,因其访问修饰符为 protected
12.6 true -
12.7 true -
12.8 False 为结构体添加构造函数时,需设置所有属性,否则编译器报错
13.1 True -
13.2 C -
13.3 A -
13.4 True -
13.5 C -
13.6 A -
13.7 B -

1.2 代码示例 - OpenAPI 规范

openapi: 3.0.1
info:
  title: FlyTomorrow required endpoints
  description: This OpenAPI file specifies the required endpoints as per the contract between FlyTomorrow.com and Flying Dutchman Airlines
  version: 1.0.0
servers:
- url: https://zork.flyingdutchmanairlines.com/v1
tags:
- name: flight
  description: Access to available flights
- name: booking
  description: Request bookings for available flights
paths:
  /flight:
    get:
      tags:
      - flight
      summary: Get all available flights
      description: Returns all available flights
      operationId: getFlights
      responses:
        200:
          description: ""
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Flight'
        404:
          description: No flights found
          content: {}
        500:
          description: Internal error
          content: {}
  /flight/{flightNumber}:
    get:
      tags:
      - flight
      summary: Find flight by flight number
      description: Returns a single flight
      operationId: getFlightByFlightNumber
      parameters:
      - name: flightNumber
        in: path
        description: Number of flight to return
        required: true
        schema:
          type: integer
          format: int32
      responses:
        200:
          description: ""
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Flight'
        400:
          description: Invalid flight number supplied
          content: {}
        404:
          description: Flight not found
          content: {}
  /booking/{flightNumber}:
    post:
      tags:
      - booking
      summary: requests a booking for a flight
      description: Request for a flight to be booked
      operationId: bookFlight
      parameters:
      - name: flightNumber
        in: path
        description: Number of flight to book
        required: true
        schema:
          type: integer
          format: int32
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Customer'
        required: true
      responses:
        201:
          description: successful operation
        500:
          description: Internal error
          content: {}
components:
  schemas:
    Airport:
      type: object
      properties:
        city:
          type: string
        code:
          type: string
    Customer:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string
    Flight:
      type: object
      properties:
        flightNumber:
          type: integer
          format: int32
        origin:
          $ref: '#/components/schemas/Airport'
        destination:
          $ref: '#/components/schemas/Airport'

1.3 流程图 - 代码编写遵循原则

graph LR
    A[代码编写] --> B[通用原则]
    A --> C[类原则]
    A --> D[方法原则]
    A --> E[变量、字段和属性原则]
    A --> F[测试原则]
    B --> B1[代码可读性强]
    B --> B2[必要时注释]
    B --> B3[提供构建和发布说明]
    B --> B4[优先使用原生功能]
    B --> B5[保持设计一致性]
    C --> C1[使用最严格访问修饰符]
    C --> C2[准确命名]
    C --> C3[遵循单一职责原则]
    C --> C4[位于合适文件夹]
    C --> C5[必要时拆分功能]
    D --> D1[使用最严格访问修饰符]
    D --> D2[准确命名并描述逻辑]
    D --> D3[遵循单一职责原则]
    D --> D4[公共方法组织调用]
    D --> D5[有单元测试支持]
    E --> E1[使用最抽象类型]
    E --> E2[避免魔法数字]
    E --> E3[使用最严格访问修饰符]
    E --> E4[验证输入参数]
    F --> F1[提供适当单元测试]
    F --> F2[可能时采用测试驱动开发]
    F --> F3[不单纯追求代码覆盖率]
    F --> F4[修复失败测试]
    F --> F5[编写最少必要代码]

2. 安装指南

2.1 .NET Framework 4.x 安装步骤

  1. 访问 https://dotnet.microsoft.com/download/dotnet-framework 。
  2. 从发布列表中选择顶部选项。
  3. 点击发布项,进入下载链接页面。
  4. 点击“Download .NET Framework 4.[version] Developer Pack”下载安装程序。
  5. 运行安装程序安装 .NET Framework。

2.2 .NET Core 3.x 安装步骤

  1. 访问 https://dotnet.microsoft.com/download/dotnet-core 。
  2. 选择最新版本(如 .NET Core 3.1.2)。
  3. 点击相应发布项,进入下载页面。
  4. 选择 Windows 或 macOS 的 SDK 安装程序(Linux 可查看包管理器说明),也可选择下载所有平台的二进制文件。
  5. 运行下载的安装程序安装 .NET Core 3。

2.3 Visual Studio Community 安装步骤

  1. 访问 https://visualstudio.microsoft.com/vs/ 。
  2. 从“Download Visual Studio”下拉列表中选择“Visual Studio Community”,下载 Visual Studio 安装程序。
  3. 启动安装程序,选择以下两个工作负载:
    • ASP.NET 和 Web 开发
    • .NET Core 跨平台开发
  4. 点击右下角下载按钮,安装 Visual Studio。

2.4 Visual Studio for Mac 安装步骤

  1. 访问 https://visualstudio.microsoft.com/vs/mac/ 。
  2. 点击“Download Visual Studio for Mac”按钮,下载安装程序。
  3. 运行安装程序,完成安装。

2.5 Visual Studio Code 安装步骤

  1. 访问 https://code.visualstudio.com/ 。
  2. 点击“Download for [platform]”按钮,下载安装程序。
  3. 运行安装程序,完成安装。
  4. 首次编写 C# 代码或打开 C# 解决方案时,接受下载 C# 包的提示。

2.6 本地运行 Flying Dutchman Airlines 数据库步骤

  1. 安装 SQL Server Developer Edition:访问 https://www.microsoft.com/en-us/sql-server/sql-server-downloads ,下载并安装 Developer 版本。
  2. 安装 SQL Server Management Studio (SSMS):访问 https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver15 ,下载并安装。
  3. 创建新的本地 SQL Server 实例:打开 SQL Server 安装中心,选择“Installation” -> “New SQL Server stand-alone installation or add features to an existing installation”,按照安装向导操作,记录登录凭据。
  4. 启动 SSMS,在连接对话框中选择 SQL 实例并填写连接信息。
  5. 连接成功后,在“Object Explorer”中右键点击“Databases”,选择“Import Data-Tier Application”。
  6. 在“Import Settings”窗口中,选择“Import from local disk”,浏览到数据库文件 (FlyingDutchmanAirlinesDatabase.bacpac)。
  7. 可选择重命名数据库,完成向导后,数据库导入成功。
  8. 若导入失败,在 SQL 实例的主数据库中运行以下命令:
    • sp_configure 'contained database authentication', 1; GO RECONFIGURE; GO;
    • 若出现“GO”关键字语法问题,可使用 EXEC sp_configure 'contained database authentication', 1; RECONFIGURE;
  9. 遇到连接字符串时,替换为本地 SQL 实例的正确连接字符串。

2.7 阅读列表

领域 推荐书籍/资料
.NET Core Metzgar, Dustin .NET Core in Action (Manning, 2018)
.NET Standard .NET Standard specification. The latest version of this document can be found at github.com/dotnet/standard/tree/master/docs/versions.
ASP.NET Lock, Andrew ASP.NET in Action (Second Edition; Manning, 2020)
C# Standard ECMA - 334 C# Language Specification; Wagner, Bill Effective C#. (Second Edition; Microsoft Press, 2016); Skeet, Jon C# In - Depth. (Fourth Edition; Manning, 2019)
COM/INTEROP Clark, Jason Calling Win32 DLLs in C# with P/Invoke. (MSDN Magazine; July 2003); Clark, Jason P/Invoke Revisited. (MSDN Magazine; October 2004)
通用语言运行时 (CLR) Richter, Jeffrey CLR Via C#. (Fourth Edition; Microsoft Press, 2012)
编译器 Aho, Alfred V., Lam, Monica S., Sethi, Ravi, and Ullman, Jeffrey D.’s Compilers: Principles, Techniques & Tools. (Second Edition; Pearson Education, 2007)
并发编程 Duffy, Joe Concurrent Programming on Windows. (Addison - Wesley, 2008)
数据库和 SQL Cornell University Relational Databases Virtual Workshop at https://cvw.cac.cornell.edu/databases/; Takahashi, Mana and Azumas, Shoko, and Trend - Pro Co., Ltd. The Manga Guide to Databases. (No Starch Press, 2009); Hunt, Andrew and Thomas, Dave The Pragmatic Programmer. (Addison Wesley, 1999)
依赖注入 Fowler, Martin Inversion of Control Containers and the Dependency Injection pattern; Martin, Robert C. OO Design Quality Metrics, An Analysis of Dependencies; Van Deursen, Steven and Seemann, Mark Dependency Injection Principles, Practices, and Patterns. (Second Edition; Manning, 2019)
设计模式 Gamma, Eric and Helm, Richard and Johnson, Ralph and Vlissides, John Design Patterns: Elements of Reusable Object - Oriented Software. (Addison - Wesley, 1994); Martin, Robert C. and Martin, Micah Agile Principles, Patterns, and Practices in C#. (Prentice Hall, 2006); Freeman, Eric and Robson, Elisabeth and Sierra, Kathy and Bates, Bert Head First: Design Patterns. (O’Reilly, 2004)
ENIAC Dyson, George Turing’s Cathedral: The Origins of the Digital Universe. (Vintage, 2012)
泛型 Skeet, Jon C# In - Depth. (Fourth Edition; Manning, 2019)
图论 Trudeau, Richard J. Introduction to Graph Theory. (Second Edition; Dover Publications, 1994)
HTTP Pollard, Barry HTTP/2 in Action. (Manning, 2019); Berners - Lee, Tim Information Management: A Proposal. (French Conseil Européen pour la Recherche Nucléaire; CERN, 1990); Berners - Lee, Tim and Fielding, Roy and Frystyk, Henrik Hypertext Transfer Protocol – HTTP/1.0. (Internet Engineering Task Force; IETF, 1996)
Kubernetes 和 Docker Lukša, Marko Kubernetes in Action. (Second Edition; Manning, 2020); Stoneman, Elton Learn Docker in a Month of Lunches. (Manning, 2020); Davis, Ashley Bootstrapping Microservices with Docker, Kubernetes, and Terraform. (Manning, 2020)
数学 Knuth, Donald The Art of Computer Programming, Volume 1: Fundamental Algorithms (Addison Wesley Longman, 1977); Hofstadter, Douglas R. Gödel, Escher, Bach: An Eternal Golden Braid. (Basic Books, 1977); Alama, Jesse and Korbmacher, Johannes The Stanford Encyclopedia of Philosophy, The Lambda Calculus; Conery, Rob The Imposter’s Handbook: A CS Primer for Self - Taught Programmers. (Rob Conery, 2017)
MATLAB Hamming, Richard Numerical Methods for Scientists and Engineers. (Dover Publications, 1987); Gilat, Amos MATLAB: An Introduction with Applications (Sixth Edition; Wiley, 2016)
微服务 Gammelgaard, Christian Horsdal Microservices in .NET Core. (Manning, 2020); Newman, Sam Building Microservices: Designing Fine - Grained Systems. (O’Reilly Media, 2015); Richardson, Chris Microservices Patterns. (Manning, 2018); Siriwardena, Prabath and Nuwan Dias Microservices Security in Action. (Manning, 2020)
操作码和汇编 BBC Bitesize: Computer Science – Binary and data representation (Instructions) at https://www.bbc.co.uk/bitesize/guides/z2342hv/revision/1
红黑树 Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford Introduction to Algorithms, Chapter 13: “Red - Black Trees”, Third Edition (Massachusetts Institute of Technology, 2009); Galles, David Red - Black Tree Visualizer https://www.cs.usfca.edu/~galles/visualization/RedBlack.html; Wilt, Nicholas Classic Algorithms in C++: With New Approaches to Sorting, Searching, and Selecting. (Wiley, 1995)
重构 Fowler, Martin Refactoring: Improving the Design of Existing Code. Addison - Wesley, 1999
关注点分离 Dijkstra, Edsger The Role of Scientific Thought in Selected Writings on Computing: A Personal Perspective. (Springer - Verlag, 1982); Martin, Robert C. Clean Code: A Handbook of Agile Software Craftsmanship. (Prentice - Hall, 2008); Constantine, Larry and Yourdon, Edward Structured Design: Fundamentals of a Discipline of Computer Program and System Design. (Prentice - Hall, 1979)
单一职责原则 Martin, Robert C. The Single Responsibility Principle (https://blog.cleancoder.com/uncle - bob/2014/05/08/SingleReponsibilityPrinciple.html)
里氏替换原则 Liskov, Barbara H. and Wing, Jeannette M. A Behavioral Notion of Subtyping. (ACM Transactions on Programming Languages and Systems (TOPLAS), 1994)
单元测试 Khorikov, Vladimir Unit Testing Principles, Practices, and Patterns. (Manning, 2020); Osherove, Roy The Art of Unit Testing. (Second Edition; Manning, 2013); Kaner, Cem and Bach, James and Pettichord, Bret Lessons Learned in Software Testing: A Context - Driven Approach. (Wiley, 2008)
Visual Studio Johnson, Bruce Professional Visual Studio 2017. (Wrox, 2017); Johnson, Bruce Essential Visual Studio 2019: Boosting Development Productivity with Containers, Git, and Azure Tools. (Apress, 2020)

3. 代码编写原则详解

3.1 通用原则

通用原则是代码编写的基础准则,它确保代码具有良好的可读性和可维护性。具体原则如下:
- 代码可读性强 :代码应像叙事一样易于理解,编写代码是为了让人读懂,而非仅让机器执行。
- 必要时注释 :仅在必要时对代码进行注释,好的代码应能自我解释。
- 提供构建和发布说明 :清晰说明代码库的构建和发布步骤,方便团队协作和部署。
- 优先使用原生功能 :除非有充分理由,否则应使用原生功能,避免重复造轮子。
- 保持设计一致性 :在设计模式、文档和命名约定上保持一致,避免开发过程中随意更改。

3.2 类原则

类是面向对象编程的重要组成部分,遵循以下原则可以使类的设计更加合理:
- 使用最严格访问修饰符 :尽量使用最严格的访问修饰符,限制类的可见性。
- 准确命名 :类名应准确反映其功能。
- 遵循单一职责原则 :一个类应只负责一项特定的操作。
- 位于合适文件夹 :类应存放在项目中合适的文件夹内。
- 必要时拆分功能 :如果类的功能过于复杂,应将其拆分为多个类。

3.3 方法原则

方法是实现具体功能的代码块,以下原则有助于提高方法的质量:
- 使用最严格访问修饰符 :和类一样,方法也应使用最严格的访问修饰符。
- 准确命名并描述逻辑 :方法名应准确描述其内部逻辑。
- 遵循单一职责原则 :一个方法应只执行一项通用操作或收集相关信息。
- 公共方法组织调用 :如果方法是公共的,应只负责调用其他小方法并组织输出。
- 有单元测试支持 :为方法编写单元测试,覆盖主要的成功和失败逻辑分支。

3.4 变量、字段和属性原则

变量、字段和属性的合理使用可以提高代码的灵活性和可维护性:
- 使用最抽象类型 :尽量使用接口而不是具体类型,促进多态性和里氏替换原则的应用。
- 避免魔法数字 :不使用未命名的常量,将其赋值给有意义的变量。
- 使用最严格访问修饰符 :尽可能限制变量、字段和属性的访问范围,可设为只读或常量。
- 验证输入参数 :对输入参数进行验证,避免空指针异常和无效状态操作。

3.5 测试原则

测试是保证代码质量的重要手段,遵循以下原则可以使测试更加有效:
- 提供适当单元测试 :为代码编写适当的单元测试。
- 可能时采用测试驱动开发 :在可能的情况下,采用测试驱动开发的方式。
- 不单纯追求代码覆盖率 :测试的目标是防止意外副作用和验证需求假设,而非单纯追求代码覆盖率。
- 修复失败测试 :如果测试失败,应及时修复。
- 编写最少必要代码 :编写最少的代码来满足所有测试,减少维护工作量。

以下是这些原则的关系图:

graph LR
    A[代码编写原则] --> B[通用原则]
    A --> C[类原则]
    A --> D[方法原则]
    A --> E[变量、字段和属性原则]
    A --> F[测试原则]
    B --> B1[可读性强]
    B --> B2[必要注释]
    B --> B3[构建说明]
    B --> B4[原生优先]
    B --> B5[设计一致]
    C --> C1[访问严格]
    C --> C2[命名准确]
    C --> C3[单一职责]
    C --> C4[位置合适]
    C --> C5[功能拆分]
    D --> D1[访问严格]
    D --> D2[命名准确]
    D --> D3[单一职责]
    D --> D4[公共组织]
    D --> D5[单元测试]
    E --> E1[抽象类型]
    E --> E2[无魔法数]
    E --> E3[访问严格]
    E --> E4[参数验证]
    F --> F1[单元测试]
    F --> F2[测试驱动]
    F --> F3[非覆盖率]
    F --> F4[修复测试]
    F --> F5[最少代码]

4. 总结

在编程过程中,掌握相关的知识点和遵循正确的代码编写原则是非常重要的。通过对一系列问题答案的了解,可以加深对编程概念的理解。同时,按照正确的安装步骤安装所需的开发环境和工具,能够为编程工作提供良好的基础。

在代码编写方面,遵循通用、类、方法、变量和测试等原则,可以提高代码的质量、可读性和可维护性。此外,参考推荐的阅读列表,可以进一步拓宽编程知识面,提升编程技能。

希望这些内容对编程学习者和开发者有所帮助,让大家在编程的道路上更加顺利。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值