文章

C#之类(Class)

using System;

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
            var Student = new Students(1,"Page");//学生类的实例
            Student.Report();
        }
    }
    class Students    //一个学生类
    {
        public Students(int ID,string Name)  //输入ctor+两次TAB 创建自定义构造器(构造函数)

        {
            this.ID = ID;
            this.Name = Name;
            Console.WriteLine($"My Name is {Name},My ID is #{ID},We Class ALL Students Count is {Amount}");
        }
        static Students() //静态构造器
        {
            Amount = 100;
        }
        public static int Amount { get; set; }//静态成员(静态是关于类的,这里是学生总数)
        public int ID { get; set; } //prop+两次TAB快速创建属性
        public string Name { get; set; }
        public void Report()
        {
            Console.WriteLine("Hello!");
        }
        ~Students()//析构函数
        {
            Console.WriteLine("Bye Bye!");
        }
    }
}

原文来自:C#之类(Class),尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论