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

    1.泛型

    优点:避免成员膨胀或者类型膨胀

    看下面的代码:

    namespace TestClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                 
                Console.ReadKey();
            }
        } 
        public class Apple
        {
            public string Corlor { get; set; }
          
        }
        public class AppleBox
        {
            public Apple apple { get; set; }
        }
        public class Book
        {
            public string Name { get; set; }
    
        }
        public class BookBox
        {
            public Book book { get; set; }
        } 
    }

    从上面的代码可以看出,如果以后要是水果种类一多,就会造成类型膨胀。所以我们想可以只用一个盒子类,把所有的水果等放到这一个盒子类中包装,

    比如下面:

     public class Box
        {
            public Book book { get; set; }
            public Apple apple { get; set; }
    ..... }

    但是这样又会造成成员膨胀。

    所以我们可以通过泛型来实现。

    namespace TestClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Apple apple = new Apple() { Color=""};
                //泛型的特化
                Box<Apple> boxApple = new Box<Apple>(){ box =apple}; 
                 
                Console.WriteLine(boxApple.box.Color);
                Console.ReadKey();
            }
        }
        public class Apple
        {
            public string Color { get; set; } 
        }
        public class Book 
        {
            public string Name { get; set; } 
        }
        /// <summary>
        /// 泛型类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class Box<T>
        {
            public T box { get; set; }
            
        } 
    }

    (2)接口泛型

     class Program
        {
            static void Main(string[] args)
            {
                //泛型的特化
                Student<ulong> student = new Student<ulong>()
                {
                    ID = 123231231
                };
                Console.WriteLine(student.ID);
                Console.ReadKey();
            }
        }
        interface IUmique<T>
        {
            T ID { get; set; }
        }
    
        class Student<T> : IUmique<T>
        {
            public T ID { get; set; }
    
        }

    有时候派生类继承的时候就确认类要用哪个类型:

     class Program
        {
            static void Main(string[] args)
            {
                
                Student student = new Student()
                {
                    ID = 123231231,
                    Name="cehsi"
                };
                Console.WriteLine(student.ID);
                Console.ReadKey();
            }
        }
        interface IUmique<T>
        {
            T ID { get; set; }
        }
    
        class Student: IUmique<ulong>
        {
            public ulong ID { get; set; }
            public string Name { get; set; }
    
        }

    2.partial类

    优点:减少类的派生

    Partial是局部类型的标志。局部类型可以实现将一个类、结构或接口分成几个部分,分别放在在几个不同的.cs文件中(当然也可以放在同一个.cs文件中)。在程序进行编译之后,将会合并成一个完整的类。

     public partial class Test
        {
            public int Id { get; set; }
        }
        public partial class Test
        {
            public void TestMethod()
            {
    
            }
        }

    编译之后:

        public partial class Test
        {
            public int Id { get; set; }
            public void TestMethod()
            {
    
            }
        }

    3.枚举类型

    枚举是用户定义的整数类型, 是一组命名整型常量。它是强类型的,与类同级,通常用来表示一组常量

    可指定类型有:byte、sbyte、short、ushort、int、uint、long、ulong

    除了正常的枚举用法之外,还有位枚举

     class Program
        {
            static void Main(string[] args)
            {
                //按位OR运算符,二进制中只要有一位是1,结果就是1.
                var skills = Skill.Cook | Skill.Drive | Skill.Program | Skill.Ride | Skill.Teach; 
                Console.WriteLine(skills);//31
                //是否会做饭
                Console.WriteLine((skills&Skill.Cook)>0);
                //是否会飞
                Console.WriteLine((skills & Skill.Fly) > 0);
                Console.ReadKey();
            }
        }
    
        enum Skill
        {
            Drive=1,
            Cook=2,
            Program=4,
            Teach=8,
            Ride=16,
            Fly = 32,
        }

    结果:

    31
    True
    False

    4.结构体

    结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。

      class Program
        {
            static void Main(string[] args)
            {
                Student student = new Student() { ID = 1, Name = "test1" };
                Student student2 = student;// new Student() { ID = 1, Name = "student2" };
                student2 = new Student() { ID = 1, Name = "student2" };
                Console.WriteLine(student2.ID + " :" + student2.Name);
                Console.WriteLine(student.ID + " :" + student.Name);
                Console.ReadKey();
            }
        }
        struct Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

    结果:

    1 :student2
    1 :test1

    因为结构体是值类型的,赋值的时候存储的是值信息,不是地址。

    注意:结构体类型只能继承自接口。不能有显式的无参构造器。

  • 相关阅读:
    pku3734Blocks
    STLmultiset
    zoj 2744
    EXCEL vba 插入图片的大小裁剪尺寸移动和旋转的设置和指定
    C# WinForm下Excel导入导出
    日期格式校验
    vb获取目录下所有文件夹名称的方法
    批量 生成 word 多线程
    Java中验证日期时间格式
    递归绑定树形菜单
  • 原文地址:https://www.cnblogs.com/anjingdian/p/13200705.html
Copyright © 2011-2022 走看看