美文网首页
C# 结构、枚举、接口

C# 结构、枚举、接口

作者: 李霖弢 | 来源:发表于2019-09-30 16:09 被阅读0次

    结构 Struct

    结构是值类型,进行栈分配而非堆分配。
    结构在分配时进行复制,对新副本所做的任何修改不会更改原始副本的数据。
    结构类型不支持显式继承自另一个结构或类(因此成员不能指定为 abstractvirtualprotected),但可以继承自接口。
    结构的字段无法赋予初值。
    结构没有默认的构造函数,但可以自行添加。
    结构没有析构函数。
    不使用 new 操作符即可实例化结构。

    struct Books
    {
       public string title;
    };  
    ...
    Books Book1; 
    Book1.title = "C Programming";
    
    struct sPoint
    {
        public int num1;
        public int num2;
        public sPoint(int num1, int num2)
        {
            this.num1 = num1;
            this.num2 = num2;
        }
    }
    
    class Point
    {
        public int num1;
        public int num2;
        public Point(int num1, int num2)
        {
            this.num1 = num1;
            this.num2 = num2;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            sPoint sA = new sPoint(10, 10);
            sPoint sB = sA;
            sA.num1 = 20;
            Console.WriteLine(sA.num1);//20
            Console.WriteLine(sB.num1);//10
            Point A = new Point(10, 10);
            Point B = A;
            A.num1 = 20;
            Console.WriteLine(A.num1);//20
            Console.WriteLine(B.num1);//20
            Console.ReadKey();
        }
    }
    

    枚举 Enum

    枚举是一组命名整型常量的独特值类型
    枚举存储格式和可取值范围由其基础类型决定,默认为int
    默认从 0 开始自动按 1 递增,但也可以自行指定。
    枚举值与整形常量可以通过显式转换相互转换。

    enum Day
    {
        Sun,
        Mon = 2,
        Tue,
        Wed = Mon + 2
    }
    class Program
    {
        static void Main(string[] args)
        {
            var num = (int)Day.Tue;
            var day = (Day)4;
            Console.WriteLine(Day.Tue);
            Console.WriteLine(num);
            Console.WriteLine(day);
        }
    }
    
    使用 System.Enum 方法发现和操作枚举值
    foreach (int i in Enum.GetValues(typeof(Day)))
        Console.WriteLine(i);//0 2 3 4
    
    foreach (string str in Enum.GetNames(typeof(Day)))
        Console.WriteLine(str);//Sun Mon Tue Wed
    

    接口 Interface

    接口定义了继承接口的类和结构的必须实现的成员,包括属性、方法和事件和索引器。接口只包含成员的声明(抽象方法)而无实现。
    接口可以实现多重继承,通过,分隔多个继承的接口。
    如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口成员。
    接口声明默认是public,通常接口命令以 I 字母开头。
    接口成员会自动成为公共成员,不能包含任何访问修饰符。 成员也不能是静态成员

    interface IMyInterface
    {
        void MethodToImplement();
    }
    
    显式接口成员

    接口必须与实现它的类对象一起初始化。
    通常实现的接口成员为公共成员,而显式接口成员只能通过接口类型的实例进行访问。
    实现接口的类或结构的实例,可以转换成该接口类型。

    class Program
    {
        static void Main(string[] args)
        {
            var boy = new Boy("小明");
            boy.Say();//My name is 小明
            //dog.Shout();//出错
    
            IHuman girl = boy;
            girl.Say();//My name is 小明
            girl.Shout();//WOWOWOW
    
            IHuman child = new Boy("大志");
            child.Say();//My name is 大志
            child.Shout();//WOWOWOW
    
            Boy child2 = (Boy)child;
            child2.Say();//My name is 大志
            //child2.Shout();//出错
    
            Console.ReadLine();
        }
    }
    interface IAnimal{
        void Shout();
    }
    interface IHuman:IAnimal
    {
        void Say();
    }
    class Boy: IHuman
    {
        readonly string name;
        public void Say()
        {
            Console.WriteLine($"My name is {name}");
        }
        void IAnimal.Shout()//显式接口成员
        {
            Console.WriteLine("WOWOWOW");
        }
        public Boy(string name)
        {
            this.name = name;
        }
    }
    

    显式接口成员还可用于类同时实现的两个接口里存在相同名称成员的情况

    interface ILeft
    {
        int P { get;}
    }
    interface IRight
    {
        int P();
    }
    class Middle : ILeft, IRight
    {
        public int P() { return 0; }
        int ILeft.P { get { return 0; } }
    }
    

    相关文章

      网友评论

          本文标题:C# 结构、枚举、接口

          本文链接:https://www.haomeiwen.com/subject/lweiyctx.html