diff --git a/C#核心/Lesson21_面向对象相关-命名空间/Lesson21_面向对象相关-命名空间.csproj b/C#核心/Lesson21_面向对象相关-命名空间/Lesson21_面向对象相关-命名空间.csproj new file mode 100644 index 0000000..1f77c28 --- /dev/null +++ b/C#核心/Lesson21_面向对象相关-命名空间/Lesson21_面向对象相关-命名空间.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson21_面向对象相关_命名空间 + enable + enable + + + diff --git a/C#核心/Lesson21_面向对象相关-命名空间/Program.cs b/C#核心/Lesson21_面向对象相关-命名空间/Program.cs new file mode 100644 index 0000000..ccc5bad --- /dev/null +++ b/C#核心/Lesson21_面向对象相关-命名空间/Program.cs @@ -0,0 +1,98 @@ +namespace Lesson21_面向对象相关_命名空间 +{ + using Lesson21_面向对象相关_命名空间.MyGame.UI; + using MyGame;//引用命名空间 + using MyGame2; + #region 知识点一 命名空间基本概念 + //概念 + //命名空间是用来组织和重用代码的 + //作用 + //就像是一个工具包,类就像是其中的工具,都是声明在命名空间中的 + #endregion + + #region 知识点二 命名空间的使用 + //基本语法 + //namespace 命名空间 + //{ + // 类 + //} + namespace MyGame + { + class GameObject + { + + } + } + namespace MyGame + { + class Player : GameObject//同一命名空间中可以直接使用 + { + + } + } + #endregion + + #region 知识点四 不同命名空间中允许有同名类 + //eg: + namespace MyGame2 + { + class GameObject + { + + } + } + //如果不同命名空间有同名类且都被使用 + //只能通过 指明出处 来使用 + #endregion + + #region 知识点五 命名空间可以包裹命名空间 + namespace MyGame + { + namespace UI + { + class Image + { + + } + } + namespace Game + { + class Player : GameObject + { + + } + } + } + #endregion + + #region 知识点六 关于修饰类的访问修饰符 + //public — 命名空间中的类 默认为 public + //internal — 只能在该程序集中使用 + //abstract — 抽象类 + //sealed — 密封类 + //partial — 分部类 + #endregion + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + #region 知识点三 不同命名空间中相互使用 需要引用命名空间或指明出处 + //目前在 Lesson21_面向对象相关_命名空间 这个命名空间中 + //如果要使用 MyGame 命名空间中的类 + + //需要引用命名空间(在最顶上和using system;一起) + //GameObject g = new GameObject(); + + //或者指明出处 + //命名空间.类名 + MyGame.GameObject g2 = new MyGame.GameObject(); + MyGame2.GameObject g3 = new MyGame2.GameObject(); + + MyGame.UI.Image img = new MyGame.UI.Image(); + + Image img0 = new Image();//因为引用了 MyGame.UI 命名空间 + #endregion + } + } +} diff --git a/C#核心/Lesson21_面向对象相关-命名空间练习/Lesson21_面向对象相关-命名空间练习.csproj b/C#核心/Lesson21_面向对象相关-命名空间练习/Lesson21_面向对象相关-命名空间练习.csproj new file mode 100644 index 0000000..feaa4b0 --- /dev/null +++ b/C#核心/Lesson21_面向对象相关-命名空间练习/Lesson21_面向对象相关-命名空间练习.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson21_面向对象相关_命名空间练习 + enable + enable + + + diff --git a/C#核心/Lesson21_面向对象相关-命名空间练习/Program.cs b/C#核心/Lesson21_面向对象相关-命名空间练习/Program.cs new file mode 100644 index 0000000..74ce111 --- /dev/null +++ b/C#核心/Lesson21_面向对象相关-命名空间练习/Program.cs @@ -0,0 +1,24 @@ +namespace Lesson21_面向对象相关_命名空间练习 +{ + namespace UI + { + class Image + { + } + } + namespace Graph + { + class Image + { + } + } + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + UI.Image img1 = new UI.Image(); + Graph.Image img2 = new Graph.Image(); + } + } +}