初始化对象的几种情况

简介:

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();
        }
    }
}

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6803195.html,如需转载请自行联系原作者
相关文章
|
4月前
|
C语言
C11中类成员变量定义时初始化问题
C11中类成员变量定义时初始化问题
29 0
|
6月前
|
Java 编译器 C++
C++冷知识:构造函数初始化时,为什么使用 : 而不是使用作用域内初始化对象?
在这个例子中,初始化列表中的成员变量顺序与类定义中的顺序不一致,可能会导致未定义的行为。 如果成员变量是const或引用类型,必须在初始化列表中进行初始化,否则会导致编译错误。
38 0
|
11月前
通过构造方法使属性初始化
通过构造方法使属性初始化
75 0
|
11月前
|
Java 编译器
对象的构造及初始化
对象的构造及初始化
59 0
|
12月前
|
安全 编译器 数据安全/隐私保护
对象的动态创建和销毁以及对象的复制,赋值
🐰对象的动态创建和销毁 🐰对象的复制 🐰对象的赋值
|
Java 测试技术
成员变量初始化的问题
成员变量初始化的问题
|
网络安全 数据安全/隐私保护
M12-2 初始化
M12-2 初始化
104 0
|
缓存 安全 Java
仅且仅创建一次对象
此篇算是对《voliatile,synchronized,cas》理论的一种实践
118 0
|
数据安全/隐私保护
|
数据库 C++
确定对象使用前已先被初始化
确定对象使用前已先被初始化
140 0