class1.cs

来自「C#2005 实例源代码」· CS 代码 · 共 37 行

CS
37
字号
using System;

namespace Example_Static
{
	/// <summary>
	/// 学生类
	/// </summary>
	public class Student
	{	
		public static int nCount;
		public string strName;
	
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="_strName">学生姓名</param>
		public  Student(string _strName)
		{
			nCount++;				//静态属性:
			this.strName=_strName;
		}

		/// <summary>
		/// 主函数
		/// </summary>
		static void Main(string[] args)
		{		
			Student s=new Student("张三");
			//Console.WriteLine(s.nCount);		//错误,不能在实例中引用静态属性
			Console.WriteLine(Student.nCount);	//输出:1

			Student s2=new Student("张三");			
			Console.WriteLine(Student.nCount);	//输出:1
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?