zoukankan      html  css  js  c++  java
  • c#之枚举,结构体

    1.枚举

    2.结构体

    例子1:    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _038_结构体 {
        //我们可以把结构体当成,几个类型组成了一个新的类型
        //比如下面的这个就是使用了3个float类型的变量,来表示一个坐标类型
        struct Position//position 表示结构体名 
        {
            public float x;
            public float y;
            public float z;
        }
    
        enum Direction
        {
            West,
            North,
            East,
            South
        }
    
        struct Path
        {
            public float distance;
            public Direction dir;
        }
        class Program {
            static void Main(string[] args)
            {
                //通过三个float类型的变量来表示一个敌人的坐标
                //float enemy1X = 34;
                //float enemy1Y = 1;
                //float enemy1Z = 34;
    
    
                //float enemy2X = 34;
                //float enemy2Y = 1;
                //float enemy2Z = 34;
    
                //当使用结构体声明变量的时候,相当于使用结构体中所有的变量去声明
                //Position enemy1Position;
                //enemy1Position.x = 34;//可以通过.加上属性名来访问结构体中指定的变量
                ////使用结构体让程序变得更清晰
                //Position enemy2Position;
    
                Path path1;
                path1.dir = Direction.East;
                path1.distance = 1000;
            }
        }
    }
    
    
    例子2:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _012结构体
    {
        public struct Student
        {
    
            public string _name;            //public 是访问修饰符,访问范围是公共的,一般条件满足别处都可以调用
            public Sex _sex;
            public int _age;
    
        }
        public enum Sex
        {
            男,
            女
    
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                //大学管理系统 来一个就需要录入信息,信息有姓名,性别,年龄。。。。。。
                //string zsName = "张三";
                /ar zsSex = '';
                //int zsAge = 20;
    
                //string lsName = "李四";
                /ar lsSex = '';
                //int lsAge = 25;
    
                //string wwName = "王五";
                /ar wwSex = '';
                //int wwAge = 26;
                //学校招了10000人,需要定义30000个变量
                //public struct 结构名{ 字段,属性,方法  }
    
                Student zsPerson;
                zsPerson._name = "张三";
                zsPerson._sex = Sex.男;
                zsPerson._age = 20;
                Student lsPerson;
                lsPerson._name = "李四";
                lsPerson._sex = Sex.女;
                lsPerson._age = 25;
                Console.WriteLine(lsPerson._age);
    
    
    
            }
        }
    }
    枚举:代码规范,把同类放在一起
     public enum Sex
        {
            男,//默认代表0
            女=10,
            middle
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                //大学管理系统:现在招生,文法系招生性别记录男女,机电系性别记录爷们娘们
                //数学系记录1,0  国贸记录boy girl
                //学校下达一个规定,性别只能用男女,其他学院必须调用学校教务处的性别,不能随便自己定义,于是产生了枚举:规范代码,把同类的放在一起
                //public enum 枚举名{ 值,值,值}
    
                #region 枚举转换Int
                   //Sex math = Sex.男;
                   //int num = (int)math;
                   //Console.WriteLine(num);
                #endregion
                #region int转枚举
                //int num = 11; 
                //Sex s = (Sex)num;//int转换枚举后,如果有对应的枚举,就显示其内容,如果没有就显示源数字
                //Console.WriteLine(s);
                #endregion
                #region String转枚举
                //string str = "middle";
                //Sex o= (Sex)Enum.Parse(typeof(Sex),str);
                //Console.WriteLine(o);
                #endregion
                #region 把枚举转换为字符串
                Sex s = Sex.middle;
                string str = s.ToString();
                Console.WriteLine(s);
                #endregion
            }
        }
    例子:
    public enum Season
        {
    
    
           春, 
        夏,
       秋,
    冬 }
    class Program { static void Main(string[] args) { Console.WriteLine("请输入季节"); string season = Console.ReadLine(); Season s; switch (season) { case "": case "": case "": case "": s = (Season)Enum.Parse(typeof(Season), season); Console.WriteLine(s); break; default: Console.WriteLine("您输入的不是季节"); break; } } }
    例子:
    using System;
    
    namespace _002_student结构
    {
        public struct Student
        {
            public long _nums;
            public string _name;
            public Sex _sex;
            public int _grade;
        }
        public enum Sex
        {
            男,
            女
        }
        class Program
        {
            static void Main(string[] args)
            {
                Student person1;
                person1._nums = 154090201026;
                person1._name = "huangwei";
                person1._sex = Sex.男;
                person1._grade = 100;
    
                Console.WriteLine(person1._name);
                Student person2;
                person2._nums = 154210;
                person2._name = "alin";
                person2._sex = Sex.女;
                person2._grade = 99;
                Console.WriteLine(person2._grade);
            }
        }
    }
    using System;
    
    namespace _009_枚举
    {
        public struct Class
        {
            public int _num;
            public string _name;
            public Sex _sex;
            public int _xuehao; 
        }
        public enum Sex
        {
            男,
            女
        }
        class Program
        {
            static void Main(string[] args)
            {
                Class person1;
                person1._num = 34;
                person1._name="黄伟";
                person1._sex = Sex.男;
                person1._xuehao = 10086;
    
                Class person2;
                person2._num = 34;
                person2._name = "小明";
                person2._sex = Sex.女;
                person2._xuehao = 55555;
    
                Console.WriteLine("姓名"+"	"+"班级人数"+"	"+"性别"+"	"+"学号");
                Console.WriteLine(person1._name+"	"+person1._num+"	"+"        "+person1._sex+"	"+person1._xuehao);
            }
        }
    }
    莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
  • 相关阅读:
    UIView+ViewController.h 点击控制器上视图,使视图push下个视图控制的封装
    Touch Demo
    layoutSubviews与drawRect
    UI NS CG CF 区别
    CALayer
    关于CALayer的困惑
    pypy 对接阿里短信平台
    mysql去掉默认值
    GCC升级
    jemalloc 测试
  • 原文地址:https://www.cnblogs.com/huang--wei/p/9425390.html
Copyright © 2011-2022 走看看