• C#


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 /*------------------------------------------------------------------------------------------------------------
     7  * 私有构造函数:
     8  *      1. 只能在类内部调用
     9  *      2. 通过类的方法返回类的实例
    10 ------------------------------------------------------------------------------------------------------------*/
    11 namespace 类_私有构造函数
    12 {
    13     class Person
    14     {
    15         private string _name;
    16         public string Name
    17         {
    18             get { return this._name; }
    19         }
    20 
    21         // 私有构造函数
    22         private Person()
    23         {
    24             Console.WriteLine("私有构造函数被调用");
    25             this._name = "Hello World!";
    26         }
    27 
    28         // 析构函数最后被自动调用
    29         ~Person()
    30         {
    31             Console.WriteLine("Person析构函数被调用了!");
    32         }
    33 
    34         // 必须使用静态方法 , 用于返回类的实例
    35         public  static Person GetInstance()
    36         {
    37             return new Person();
    38         }
    39 
    40     }
    41 
    42     class Program
    43     {
    44         static void Main(string[] args)
    45         {
    46             Person p = Person.GetInstance();
    47 
    48             Console.WriteLine("类实例的Name属性为: {0}", p.Name);
    49             Console.ReadLine();
    50         }
    51     }
    52 }

  • 相关阅读:
    ES6中对象新增方法
    ES6中字符串新增方法
    Laya 吐槽日志.
    汇编与反汇编工具
    Mac 软件下载地址
    红米手机 android4.4.4 root之路
    查看apk安装包信息
    文件搜索
    自动发表QQ空间说说
    批量格式化json
  • 原文地址:https://www.cnblogs.com/DuanLaoYe/p/5354132.html
Copyright © 2020-2023  润新知