標題:
[9-3]namespace and using
[打印本頁]
作者:
李泳霖
時間:
2023-11-29 11:40
標題:
[9-3]namespace and using
本帖最後由 李泳霖 於 2024-1-9 11:20 編輯
namespace 為一個空間,可以存放很多模板的空間,功用:分類與管理模板,如同資料夾般
如下:
Person這個模板(類別clss)是屬於Animal這個空間的
namespace Animal
{
class Person
{
public double height;
public int age;
public string name;
}
}
namespace house
{
}
複製代碼
所以當有人要使用到Person,必須使用以下方法
Animal.Person person1 = new Animal.Person();//顯得麻煩
person1.height = 150;
person1.age = 42;
person1.name = "大大";
Animal.Person person2 = new Animal.Person();
person2.height = 180;
person2.age = 20;
person2.name = "小小";
System.Console.WriteLine("姓名: "+person1.name);
System.Console.WriteLine("年齡: " + person1.age);
System.Console.WriteLine("身高: " + person1.height);
System.Console.WriteLine("==================================");
System.Console.WriteLine("姓名: " + person2.name);
System.Console.WriteLine("年齡: " + person2.age);
System.Console.WriteLine("身高: " + person2.height);
複製代碼
//class 、 object
using Animal;
using System;
Person person1 = new Person();
person1.height = 150;
person1.age = 42;
person1.name = "大大";
Person person2 = new Person();
person2.height = 180;
person2.age = 20;
person2.name = "小小";
Console.WriteLine("姓名: "+person1.name);
Console.WriteLine("年齡: " + person1.age);
Console.WriteLine("身高: " + person1.height);
Console.WriteLine("==================================");
Console.WriteLine("姓名: " + person2.name);
Console.WriteLine("年齡: " + person2.age);
Console.WriteLine("身高: " + person2.height);
複製代碼
namespace 內也可以再新增namespace
如下所示:
namespace Animal
{
namespace qq
{
class Person
{
public double height;
public int age;
public string name;
}
}
}
namespace house
{
}
複製代碼
//class 、 object
using Animal.qq;
using System;
Person person1 = new Person();
person1.height = 150;
person1.age = 42;
person1.name = "大大";
Person person2 = new Person();
person2.height = 180;
person2.age = 20;
person2.name = "小小";
Console.WriteLine("姓名: "+person1.name);
Console.WriteLine("年齡: " + person1.age);
Console.WriteLine("身高: " + person1.height);
Console.WriteLine("==================================");
Console.WriteLine("姓名: " + person2.name);
Console.WriteLine("年齡: " + person2.age);
Console.WriteLine("身高: " + person2.height);
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2