c# 入门第一课 命名空间

选择  c# 里控制台应用程序  起名字为demo0330    会自动生成一个Program.cs 文件

 在解决方案资源管理器里右键点击添加一个类,命名为MyClass.cs

using System;//  using 是关键字,代表把命名空间导进来    System是命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo033001 //namespace  是申明命名空间关键字
{
class Myclass
{

}
}
//定义一个命名空间
//小明 三年一班
//小明 四年二班
// 三年一班 四年二班 命名空间
//小明 是命名空间下面的抽象模型 也就是类

namespace ClassTreeOne
{
class XiaoMing
{
public void age()
{
Console.WriteLine("我是三年一班的小明10岁");
Console.ReadKey();
}
}
}
namespace ClassFourTwo
{
class XiaoMing
{
public void age()
{
Console.WriteLine("我是四年二班的小明11岁");
Console.ReadKey();
}
}
}

在Program.cs 写入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo033001
{
class Program
{
static void Main(string[] args)
{
ClassFourTwo.XiaoMing xm = new ClassFourTwo.XiaoMing();
xm.age(); //xm的agefang
}
}
}

第二种方法

在打开Program.cs 写入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassFourTwo;//当引用命名空间 四年二班后
//using ClassTreeOne;

namespace demo033001
{
class Program
{
static void Main(string[] args)
{
XiaoMing xiaoming = new XiaoMing();//抽象对象实例化,当引用命名空间 四年二班后,可以直接对命名空间下的类 XiaoMing进行实例化
xiaoming.age();//xiaoming的方法age年龄
}
}
}

第三种情况

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassFourTwo;//当引用命名空间 四年二班后
using ClassTreeOne;//当应用第二个命名空间 三年一班

namespace demo033001
{
class Programt
{
static void Main(string[] args)
{
ClassFourTwo.XiaoMing xiaoming = new ClassFourTwo.XiaoMing();
//抽象对象实例化,当引用两个命名空间 四年二班赫三年一班后,需要命名空间.命名空间下的类 XiaoMing进行实例化 xiaoming
xiaoming.age();//xiaoming的方法age年龄t

ClassTreeOne.XiaoMing xm = new ClassTreeOne.XiaoMing();
//抽象对象实例化,当引用两个命名空间 四年二班赫三年一班后,需要命名空间.命名空间下的类 XiaoMing进行实例化 xm
xm.age();//xiaoming的方法age年龄
}
}
}

猜你喜欢

转载自www.cnblogs.com/fanqiusha1988/p/12600007.html