|
C#程式是利用命名空間組織起來的,命名空間既做程式的內部組織系統,又用做外部組織系統,就像一個國家為了便于管理,分成多個省份一樣,
宣告命名空間
命名空間是.NET 避免名稱沖突的一種方式,例如,定義類Phone來表示手機,同時其他人也可以定義一個Phone類,只要二者不在同一個命名空間中,就不引起命名沖突,在一個命名空間中可以有多個類、結構、介面等,在同一個命名空間中,類名、結構、介面等不可重名,
- 語法格式如下:
namespace 命名空間名稱 { //命名空間主體 }如下程式:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { class Program { static void Main(string[] args) { Student Stu1; Stu1.stuNum = "14031403"; Stu1.stuName = "小明"; Stu1.stuAge = 19; Stu1.stuClass = 1; Stu1.stuGrade = 12; Console.WriteLine("結構體演示"); Console.WriteLine("學生 " + Stu1.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}",Stu1.stuNum,Stu1.stuAge,Stu1.stuGrade,Stu1.stuClass); Stu1.ChengeStudentInf("14031403", "小明", 19, 1, 13); Console.WriteLine("呼叫結構體方法演示"); Console.WriteLine("學生 " + Stu1.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}", Stu1.stuNum, Stu1.stuAge, Stu1.stuGrade, Stu1.stuClass); Student stu2 = new Student("14081408", "小王", 18, 3, 12); Console.WriteLine("呼叫結構體建構式演示"); Console.WriteLine("學生 " + stu2.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}", stu2.stuNum, stu2.stuAge, stu2.stuGrade, stu2.stuClass); Console.ReadKey(); } } public struct Student //定義結構體Student { public String stuNum; //定義結構成員表示學生學號 public String stuName; //定義結構成員表示學生名字 public int stuAge; //定義結構成員表示學生年齡 public int stuClass; //定義結構成員表示學生班級 public int stuGrade; //定義結構成員表示學生年級 public Student(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade) { //結構體的建構式,用引數給成員賦值 this.stuNum = stuNum; this.stuName = stuName; this.stuAge = stuAge; this.stuClass = stuClass; this.stuGrade = stuGrade; } public void ChengeStudentInf(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade) { //結構體的方法函式,用引數改變成員的值 this.stuNum = stuNum; this.stuName = stuName; this.stuAge = stuAge; this.stuClass = stuClass; this.stuGrade = stuGrade; } } }namespace test就是定義了test命名空間,
命名空間不過是資料型別的一種組合方式,但命名空間中所有資料型別的名稱都會自動加上該命名空間的名字作為其前綴,命名空間還可以相互嵌套,如果沒有顯示提供命名空間,則默認用該專案名稱作為命名空間,using關鍵字
把一個型別放在命名空間中,可以有效地給這個型別制定一個較長的名稱,該名稱包括型別的命名空間,后面是句點“.”和型別的名稱,
- 語法格式如下:
using 命名空間名稱;引入命名空間后,就可以在代碼中直接引入命名空間中型別的名字了,
如下:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { class Program { static void Main(string[] args) { Student Stu1; Stu1.stuNum = "14031403"; Stu1.stuName = "小明"; Stu1.stuAge = 19; Stu1.stuClass = 1; Stu1.stuGrade = 12; Console.WriteLine("結構體演示"); Console.WriteLine("學生 " + Stu1.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}",Stu1.stuNum,Stu1.stuAge,Stu1.stuGrade,Stu1.stuClass); Stu1.ChengeStudentInf("14031403", "小明", 19, 1, 13); Console.WriteLine("呼叫結構體方法演示"); Console.WriteLine("學生 " + Stu1.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}", Stu1.stuNum, Stu1.stuAge, Stu1.stuGrade, Stu1.stuClass); Student stu2 = new Student("14081408", "小王", 18, 3, 12); Console.WriteLine("呼叫結構體建構式演示"); Console.WriteLine("學生 " + stu2.stuName.ToString() + ":"); Console.WriteLine("學號:{0},年齡:{1},年級:{2},班級:{3}", stu2.stuNum, stu2.stuAge, stu2.stuGrade, stu2.stuClass); Console.ReadKey(); } } public struct Student //定義結構體Student { public String stuNum; //定義結構成員表示學生學號 public String stuName; //定義結構成員表示學生名字 public int stuAge; //定義結構成員表示學生年齡 public int stuClass; //定義結構成員表示學生班級 public int stuGrade; //定義結構成員表示學生年級 public Student(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade) { //結構體的建構式,用引數給成員賦值 this.stuNum = stuNum; this.stuName = stuName; this.stuAge = stuAge; this.stuClass = stuClass; this.stuGrade = stuGrade; } public void ChengeStudentInf(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade) { //結構體的方法函式,用引數改變成員的值 this.stuNum = stuNum; this.stuName = stuName; this.stuAge = stuAge; this.stuClass = stuClass; this.stuGrade = stuGrade; } } }using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;這就是在引入命名空間,
如果Using指令參考的兩個命名空間包含同名的類,就必須使用完整的名字,以確保編譯器知道訪問哪個型別,就像一班和二班都有一個叫黎明的學生,開校大會點名時就要說清楚是一班的還是二班的黎明,建議在大多數情況下,都至少要提供兩個嵌套的命名空間名稱,第一是公司名,第二是技術名稱或軟體包名稱,而類是其中的一個成員,
- 博主簡介:
- 工業自動化上位機軟體工程師、機器視覺演算法工程師、運動控制演算法工程師,目前從業于智能制造自動化行業, 博主郵箱:2296776525@qq.com
- 幫忙點個贊吧,哈哈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/206162.html
標籤:其他
