添加Lesson21
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson21_面向对象相关_命名空间</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson21_面向对象相关_命名空间练习</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user