• 拆箱,装箱,枚举,结构


    枚举:

    1、不能定义自己的方法

    2、它们不能实现接口

    3、不能定义属性和索引器

    4、枚举成员之间用“,”隔开

    5、枚举成员如果没有赋值,那么它的第一个值默认为0

    6、后面的成员取值是前一个成员取值+1
    7、枚举成员只能赋值为整型

    类与结构:
    1、类和结构都是创建对象的模版
    2、
    结构是值类型,类是引用类型,
    结构不能有析构函数
    3、类可以有析构函数
    结构不能声明默认构造函数(没有参数的构造函数)
    4、结构可以声明构造函数,但他们必须带参数,并且需要把所有字段都要赋值

    5、在结构中初始化实例字段是错误的,在类中是可以初始化实例字段

    6、结构的实例化可以不使用new运算符,类中必须使用new来进行实例化  

    7、结构体不能像类一样被继承   

      class Program
        {
            //装箱、拆箱
            //static void Main(string[] args)
            //{
            //    int a = 10;
            //    string s = a.ToString();//装箱
            //   static void Main(string[] args) int num = Convert.ToInt32(s);//拆箱
            //    Console.WriteLine(num);//装箱,num会先转换为字符串再进行输出
            //}

            //枚举的使用
            //static void Main(string[] args)
            //{
            //    Student stu = new Student();
            //    stu.Name = "张三";
            //    stu.Age = 23;
            //    stu.Sex = Sex.男;
            //    stu.Grade = Grade.Y2;

            //    //枚举转换为整型数据
            //    Console.WriteLine((int)(stu.Grade));
            //    stu.Introduce();

            //    //字符串转枚举
            //    string s = "S2";
            //    Grade grade = (Grade)(Enum.Parse(typeof(Grade),s));//typeof是运算符,不是方法
            //    if(grade == Grade.S2)
            //    {
            //        Console.WriteLine("转换成功!");
            //    }           
            //}

            //结构的使用
            static void Main(string[] args)
            {
                //结构使用(方法一:使用new)
                //StructStudent strStudent = new StructStudent("张三",23,"男");
                //strStudent.SayHi();

                //结构使用(方法二:不使用new,直接声明)
                StructStudent strStudent;
                strStudent.name = "张三";
                strStudent.age = 23;
                strStudent.sex = "男";
                strStudent.SayHi();
            }
        }

      /// <summary>
        /// 学员结构
        /// </summary>
        public struct StructStudent
        {
            //字段
            public string name;
            public int age;
            public string sex;

            //结构中不能显示声明无参的构造方法
            //public StructStudent() { }

            public StructStudent(string name,int age,string sex)
            {
                this.name = name;
                this.age = age;
                this.sex = sex;
            }

            //方法
            public void SayHi()
            {
                Console.WriteLine("大家好!我叫:"+this.name+" 年龄:"+this.age+" 性别:"+this.sex);
            }
        }

    /// <summary>
        /// 性别的枚举
        /// </summary>
        public enum Sex
        {
            男,女,中性
        }
        /// <summary>
        /// 年级枚举
        /// </summary>
        public enum Grade
        {
            S1,S2=5,Y2
        }
        class Student
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            public int Age { get; set; }
            public Sex Sex { get; set; }
            public Grade Grade { get; set; }

            public void Introduce()
            {
                Console.WriteLine("姓名:"+Name+" 年龄:"+Age+" 性别:"+Sex+" 年级:"+Grade);
            }
        }

  • 相关阅读:
    WPF中的brushes
    com中的线程模式(转)
    线程同步
    WPF线程
    应用程序管理(Application)
    WPF的继承结构树
    HTML技巧100例(一)
    多个网站共用一个空间的超值玩法
    用JavaScript实现浏览器地震效果
    HTML技巧100例(二)
  • 原文地址:https://www.cnblogs.com/danmao/p/3871764.html
Copyright © 2020-2023  润新知