EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

C#构造函数的Tips

构造函数

✦ 字段初始化在构造函数之前运行。
✦ 未初始化字段有一个默认值。
✦ 字段可选初始化。

 

 

var wine=new Wine();

要求Wine这个类的构造函数必须以public关键词实现,

否则var wine=new Wine();是错误的,

你可以在Wine里面定义一个静态方法,

然后用该静态方法去返回你private的构造方法,

然后你就应该以

var wine=Wine.CreateInstance();

来调用构造方法。

这样的做法,被称为工厂模式,不是单例模式。

可以用这样的方法允许只返回一个实例,来实现单例模式。

 

实例:

namespace 构造函数和工厂模式
{
    public class Wine
    {
        Wine()
        {

        }

        public static Wine CreateInstance()
        {
            return new Wine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var wine = Wine.CreateInstance();
        }
    }
}

This article was last edited at 2020-03-14 17:23:56

buying generic cialis online safe

I all the time used to study article in news papers but now as I am a user of internet so from now I am using net for articles or reviews, thanks to web.

reply

2021-07-21 17:09:30

buy cialis 5mg online

Hey There. I found your blog the use of msn. This is a very smartly written article. I'll be sure to bookmark it and return to learn more of your helpful info. Thanks for the post. I'll certainly comeback.

reply

2022-01-01 06:28:38

* *