第十五节 常量const 、 枚举enum 、结构struct 值类型和引用类型
2012-06-20 21:02
465 查看
View Code
案例1:常量 const class Program { static void Main(string[] args) { //在变量前加入const 关键字意味着他就是一个常量 //如果我们在编写程序的时候希望一个变量不能被改变,我们就定义它为常量 //常量的标识符通常全部都是大写,程序员看到全部是大写的标识符就能明白它是一个常量 const double PI= 3.14; //一旦定义为常量意味着它的值就不能改变 //PI = 3.24; Console.ReadKey(); } } ———————————————————————————————————————————————————— 案例2.1 枚举 namespace e2 { //枚举的定义方式 //访问修饰符 enum 枚举名 //{ //枚举1 //枚举2 //.......... //} //枚举类型的作用就是限定取值范围,比如说性别男和女 //枚举不能包含方法 public enum Genders { Male, Female } class Student { private Genders gender; public Genders Gender { set { gender = value; } get { return gender; } } public void Display() { Console.Write(gender); } } } —————————————————————————————————————————————————————————— 案例2.2 namespace e2 { class Program { static void Main(string[] args) { Student stu= new Student(); stu.Gender =Genders.Male; stu.Display(); Console.ReadKey(); } } } —————————————————————————————————————————————————————————————————————————————————————————————————————————————— 案例3 枚举转换 namespace e3 { public enum NIIT { MMS1=3, MMS2=4, MMS3=5, MMS4=6 } class Program { static void Main(string[] args) { Console.WriteLine("{0}", (int)NIIT.MMS1);//索引默认为0,也可自定义! Console.WriteLine("枚举转字符串:{0}",NIIT.MMS3.ToString()); NIIT m3 = (NIIT)Enum.Parse(typeof(NIIT), "MMS3"); Console.WriteLine("字符串常量转枚举:{0}",m3); Console.ReadKey(); } } } ———————————————————————————————————————————————————————— 案例4 枚举就是自定义数据类型 namespace e4 { public enum TIME //枚举就是自定义的数据类型!!! { time1 = 1, time2 = 2, time3 = 3, time4 = 4, time5 = 5, time6 = 6, time7 = 7, } class Program { static void Main(string[] args) { Console.Write("请输入:(1-7)"); int a=Convert.ToInt32(Console.ReadLine()); TIME time = (TIME)a; //枚举就是自定义的数据类型!!! switch (time) { case TIME.time1: case TIME.time2: case TIME.time3: case TIME.time4: case TIME.time5: Console.WriteLine("需要工作,泪奔......."); break; case TIME.time6: case TIME.time7: Console.WriteLine("我喜欢周末!!!"); break; default: Console.WriteLine("输入错误");break; } Console.ReadKey(); } } —————————————————————————————————————————————————————————— 案例5结构 namespace e5 { public struct StudentStruct { //结构 中不允许赋初始值给他的成员 public string name; public int age; public string hobby; public int populraity; //结构不允许有默认函数! //public StudentStruct() //{ //} //结构里还可以包含函数 public StudentStruct(string name, int age, string hobby, int populraity) { this.name=name; this.age=age; this.hobby = hobby; this.populraity = populraity; } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},爱好:{2},受欢迎度:{3}", name, age, hobby, populraity); } } class Program { static void Main(string[] args) { //不用new方式创建! StudentStruct ss1; ss1.name = "张俊杰"; ss1.age = 18; ss1.hobby = "唱唱歌"; ss1.populraity = 110; ss1.SayHi(); StudentStruct ss2 = new StudentStruct("周杰杰", 25, "打打球", 80); ss2.SayHi(); Console.ReadKey(); } } } ———————————————————————————————————————————————————— 案例6.1 值类型和引用类型 namespace e6 { struct StudentStruct { public string Name; public int Age; public string hobby; } } ———————————————————————————————————————————————————— 案例6.2 namespace e6 { class Student { public string Name; public int Age; public string hobby; } } ———————————————————————————————————————————————— 案例6.3 namespace e6 { class Program { static void Main(string[] args) { //值类型 /*结构(struct)是值类型,所以它会以传值的方法进行传递 * 以下例子,把ss1对象赋值给ss2对象,实际上是把结构ss1对象成员如:age * 将它的值复制了一份给ss2对象的age,相互之间无任何关系,所以即使改变了 * ss2对象的值,ss1对象的值也不会发生改变 * */ StudentStruct ss1 = new StudentStruct(); ss1.Age=18; StudentStruct ss2=ss1; ss2.Age=20; Console.WriteLine("ss1结构对象的age:{0},ss2结构对象的age:{1}", ss1.Age, ss2.Age); Console.WriteLine(); //---------------------------------------------------------------------------------------------------- //类 (class)是引用类型,它将会以传引用的方式进行传递 //以下例子将stu1对象赋给了stu2,实际上传递的是一个引用,即: //stu1.age 和stu2.age引用的是同一个内存空间的值,所以 //stu2.age的值发生改变,两个对象的age都发生了改变 Student stu1 = new Student(); stu1.Age = 18; Student stu2 = stu1; stu2.Age = 20; Console.WriteLine("类"); Console.WriteLine("stu1结构对象的age:{0},stu2结构对象的age:{1}", stu1.Age, stu2.Age); Console.ReadKey(); } } } —————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 案例7 判断 早上好 下午好 namespace l1 { public enum TimeOfDay { morning=1, afternoon, evening } class Program { static void Main(string[] args) { Console.Write("请输入1. morning, 2.afternoon,3.evening"); int a = int.Parse(Console.ReadLine()); TimeOfDay time = (TimeOfDay)a; switch (time) { case TimeOfDay.afternoon: Console.WriteLine("Good Afternoon!"); break; case TimeOfDay.evening: Console.WriteLine("Good Evening!"); break; case TimeOfDay.morning: Console.WriteLine("Good Morning!"); break; default: Console.WriteLine("输入错误....."); break; } Console.ReadKey(); } } } ———————————————————————————————————————————————————— 案例8结构例子 namespace l2 { public struct TeacherStruct { public string teacherName; public int teacherAge; public int yearsOfService; //public TeacherStruct(string teacherName, int teacherAge, int yearsOfService) //{ // this.teacherName = teacherName; // this.teacherAge = teacherAge; // this.yearsOfService = yearsOfService; //} public void SayHi() { Console.WriteLine("大家好,我是{0}老师。我已经在教育战线奋斗了{1}年了", teacherName, yearsOfService); } } class Program { static void Main(string[] args) { TeacherStruct ts; ts.teacherName = "张鬼"; ts.teacherAge = 35; ts.yearsOfService = 15; ts.SayHi(); Console.ReadKey(); } } }
相关文章推荐
- 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组
- 常量 枚举 装拆箱 结构 引用/值类型
- C++系列之基本数据类型、常量、变量、const、引用、typedef、枚举、类类型、编写自己的头文件
- Item 30 用enum代替int常量类型枚举,string常量类型枚举
- C#编程:枚举(enum)与结构(struct)的定义和使用方法
- 枚举类型Enum用来存放系统常量
- 结构struct 联合Union和枚举Enum的细节讨论
- struct 结构及enum枚举
- 枚举是值类型而System.Enum却是引用类型的原因
- 变量,常量,引用类型,值类型,枚举类型, 值类型和引用类型的简单区别,类型转换,装箱与拆箱
- enum 枚举类型定义常量及调用的应用举例
- C#基础精华02(静态类,值类型,引用类型,枚举,结构,ref与out)
- Mybatis通过GNDL语法引用静态常量或者枚举类型
- 枚举是值类型而System.Enum却是引用类型的原因
- C#基础精华02(静态类,值类型,引用类型,枚举,结构,ref与out)
- C++之类内不同的数据成员类型以及方法(静态,const,引用,常量引用)
- 2016-03-20枚举、结构、索引器值类型、引用类型
- csharp类、引用类型(class)与结构、值类型(struct)的区别
- 部分类、值类型、引用类型、const、readonly、const和枚举
- 枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。