• 初始化对象的几种情况


    1.默认的无参方式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoInit
    {
        public class Curry
        {
            public string MainIngredient { get; set; }
            public string Style { get; set; }
            public int Spiciness { get; set; }
    
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Curry tastyCurry = new Curry();
                // 默认无参的方式初始化
                tastyCurry.MainIngredient = "A";
                tastyCurry.Style = "B";
                tastyCurry.Spiciness = 8;
    
                Console.WriteLine("Hello,{0}",tastyCurry.Style);
                Console.ReadKey();
            }
        }
    }
    
    

    2.定义一个构造函数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoInit
    {
        public class Curry
        {
            public string MainIngredient { get; set; }
            public string Style { get; set; }
            public int Spiciness { get; set; }
    
            public Curry (string MainIngredient,string Style,int Spiciness)
            {
                this.MainIngredient = MainIngredient;
                this.Style = Style;
                this.Spiciness = Spiciness;
            }
    
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // 有参数的构造函数
                Curry tastyCurry = new Curry("A","B",8);
    
                Console.WriteLine("Hello,{0}",tastyCurry.Style);
                Console.ReadKey();
            }
        }
    }
    
    

    3.使用对象初始化器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoInit
    {
        public class Curry
        {
            public string MainIngredient { get; set; }
            public string Style { get; set; }
            public int Spiciness { get; set; }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Curry tastyCurry = new Curry
                {
                    MainIngredient = "A",
                    Style = "B",
                    Spiciness = 8
                };
    
                Console.WriteLine("Hello,{0}",tastyCurry.Style);
                Console.ReadKey();
            }
        }
    }
    
    

    对象初始化器更具有灵活性。

    4.进一步结合集合进行处理

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoInit
    {
        public class Curry
        {
            public string MainIngredient { get; set; }
            public string Style { get; set; }
            public int Spiciness { get; set; }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                List<Curry> moreCurries = new List<Curry>
                {
                    new Curry
                    {
                        MainIngredient = "A",
                        Style = "B",
                        Spiciness = 1
                    },
                    new Curry
                    {
                        MainIngredient = "C",
                        Style = "D",
                        Spiciness = 2
                    },
                    new Curry
                    {
                        MainIngredient = "E",
                        Style = "F",
                        Spiciness = 3
                    },
                };
                foreach (Curry myCurry in moreCurries)
                {
                    Console.WriteLine("Hello,{0}", myCurry.Style);
                }
                
                Console.ReadKey();
            }
        }
    }
    
    
  • 相关阅读:
    公平锁和非公平锁
    读写锁StampedLock的思想
    线程工作窃取算法
    关于SQL注入的问题以及解决方法
    简单工厂模式、工厂模式和抽象工厂模式
    RestFul的无状态规则详解
    Identity Server 4 中文文档(v1.0.0) 目录
    第3章 支持和规范
    第2章 术语
    第1章 背景
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6803195.html
Copyright © 2020-2023  润新知