C#类中的构造函数
来源:互联网 发布:数据分析师的岗位要求 编辑:程序博客网 时间:2024/06/05 09:45
1.私有构造函数
私有构造函数式一种特殊的实例构造函数,在类之外不可访问,无法实例化具有私有构造函数的类,它通常用在只包含静态成员的类中。
using System;using System.Collections.Generic;using System.Linq;using System.Text;public class mycalss{ private mycalss() { } //私有构造函数 public static int counter; public static int incrementcounter() { return ++counter; }}class mainclass{ static void Main() { mycalss.counter = 100; Console .WriteLine ("new counter {0}", mycalss .counter ); mycalss.incrementcounter(); Console.WriteLine("now counter {0}", mycalss.counter); Console.ReadLine(); }}
2.静态构造函数
静态构造函数用于初始化类,在创建第一个实例或引用任何静态方法之前,将自动调用静态构造函数来初始化类。
(1)静态构造函数既没有访问修饰符,也没有参数
(2)在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类
(3)无法直接调用静态构造函数
(4)在程序中,用户无法控制何时执行静态构造函数
(5)静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。
using System;using System.Collections.Generic;using System.Linq;using System.Text;class myclass{ static myclass() //静态构造函数 { Console.WriteLine("the static constructor invoked."); } public static void mymethod() { Console.WriteLine("mymethod invoed."); }}class mainclass{ static void Main() { myclass.mymethod(); Console.ReadLine(); }}
- C#类中的构造函数
- C#中的构造函数
- C#中的构造函数
- c#中的构造函数
- C#中的构造函数
- C#中的构造函数
- C#中的构造函数
- C#中的构造函数
- C#中的构造函数
- 《解析C#类中的构造函数》
- c#中的静态构造函数
- C#中的类型构造函数(静态构造函数)
- 类中的构造函数
- 类中的构造函数
- 类中的构造函数
- 类中的函数:构造函数
- 类中的函数:构造函数
- C#类成员-构造函数
- GoAgent不能用的解决方法
- CFileDialog 支持文件多选 自定义filter
- C++之继承与派生(3)
- 学习笔记--多线程_概述
- 模板函数的重载
- C#类中的构造函数
- clock IP使用 50M倍频至220M
- 【【【超高仿】】】迅雷播放器教程 -- 总结(14)
- ACM新手入门
- MPI 自定义通用结构和传输
- C++之多态性与虚函数
- ASP.NET MVC 05 - 使用 Ninject
- JMSWMQ2013: 为队列管理器“QM1”提供的安全性认证无效,连接方式为“Client”,主机名为“192.168.1.1(1415)”。 请检查提供的用户名和密码在您连接至的队列管理器中是否
- 交互设计师需要从什么做起?