添加Lesson19

This commit is contained in:
2025-10-23 21:00:25 +08:00
parent f77c10f1d5
commit 0fa8be7459
5 changed files with 139 additions and 0 deletions
+12
View File
@@ -77,6 +77,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多线程", "Lesso
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多线程练习", "Lesson18_多线程练习\Lesson18_多线程练习.csproj", "{D98DCA4D-7F46-4376-9BBF-F1343D058FF3}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多线程练习", "Lesson18_多线程练习\Lesson18_多线程练习.csproj", "{D98DCA4D-7F46-4376-9BBF-F1343D058FF3}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson19_预处理器指令", "Lesson19_预处理器指令\Lesson19_预处理器指令.csproj", "{B8C4D4E0-7249-4AF7-BA24-18739B80D4A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson19_预处理器指令练习", "Lesson19_预处理器指令练习\Lesson19_预处理器指令练习.csproj", "{61DA81E2-A8D8-480E-B708-3D28334D76B0}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -231,6 +235,14 @@ Global
{D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Debug|Any CPU.Build.0 = Debug|Any CPU {D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Release|Any CPU.ActiveCfg = Release|Any CPU {D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Release|Any CPU.Build.0 = Release|Any CPU {D98DCA4D-7F46-4376-9BBF-F1343D058FF3}.Release|Any CPU.Build.0 = Release|Any CPU
{B8C4D4E0-7249-4AF7-BA24-18739B80D4A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8C4D4E0-7249-4AF7-BA24-18739B80D4A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8C4D4E0-7249-4AF7-BA24-18739B80D4A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8C4D4E0-7249-4AF7-BA24-18739B80D4A9}.Release|Any CPU.Build.0 = Release|Any CPU
{61DA81E2-A8D8-480E-B708-3D28334D76B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61DA81E2-A8D8-480E-B708-3D28334D76B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61DA81E2-A8D8-480E-B708-3D28334D76B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61DA81E2-A8D8-480E-B708-3D28334D76B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,73 @@
#define Unity5
#define Unity2019
#undef Unity2019//下面#if判断Unity2019会失效
using System;
namespace Lesson19_预处理器指令
{
internal class Program
{
static void Main(string[] args)
{
#region
//编译器是一种翻译程序
//他用将元语言程序翻译为目标语言程序
//源语言程序:某种程序设计语言写成的,比如C#、Java、C++等语言写的程序
//目标语言程序:二进制数表示的为机器代码写成的程序
#endregion
#region
//预处理器指令 知道编译器 在实际编译源代码之前 对信息进行预处理
//预处理器指令 都是以 # 开头的指令
//预处理器指令不是语句,所以他们不以;分号结束
//目前我们经常用到的 折叠代码块 就是预处理器指令
#endregion
#region
//1
//#define
//定义一个符号,类似一个没有值的常量
//#undef
//取消define定义的符号,让其失效
//两者都是写在脚本文件最前面
//一般配合if指令使用 或配合特性
//2
//#if
//#elif
//#else
//#endif
//和if语句规则一样,一般配合#define指令使用
//用于告诉编译器惊醒编译代码流程控制
//如果发现有Unity4这个符号那么其中包含的代码就会被编译器翻译
//可以通过逻辑或和逻辑与进行多种符号的组合判断
#if Unity4 && !Unity2019
Console.WriteLine("使用版本Unity4编译代码");
#elif Unity2019 //undef了Unity2019 所以这里的判断是false
Console.WriteLine("使用版本Unity2019编译代码");
#elif Unity5
#warning //发出警告信息
Console.WriteLine("使用版本Unity5编译代码");
#error //发出错误信息
#endif
//3
//#warning
//#error
//告诉编译器
//是发出警告信息 还是错误信息
//编译器会在编译时显示这些信息
#endregion
}
}
}
//总结
//预处理器指令
//可以让代码还没有编译之前就可以进行一些预处理判断
//在Unity中国会用来惊醒一些平台或者版本的判断
//决定不同的版本或不同平台使用不同的代码逻辑
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,34 @@
#define Unity2017
#define Unity2020
namespace Lesson19_预处理器指令练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数字:");
int b = int.Parse(Console.ReadLine());
Console.WriteLine(Calc(a, b));
}
static int Calc(int a, int b)
{
#if Unity5
return a + b;
#elif Unity2017
return a*b;
#elif Unity2019
return a-b;
#else
return 0;
#endif
}
}
}