C#基础_类的声明
新建Clerk类。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 类的声明 { public enum Gender { 男, 女, } class Clerk { //类中可以存放字段,存放属性和方法 public string _name;//字段,命名规范,下划线加驼峰命名法 //字段可以存放多个值,变量只能存放一个值 public Gender _gender; public int _age; public string _department; public int _workYears; //静态方法只能调用静态成员 //非静态方法可以调用任何成员 public void Write() { Console.WriteLine("我叫{0},我是{1}生,今年{2}岁,我在{3}任职,我工作了{4}年",_name,_gender,_age,_department,_workYears); } } }
使用类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 类的声明 { class Program { static void Main(string[] args) { //将类实例化,就是将类指定黑给某个对象 Clerk zsClerk = new Clerk(); zsClerk._name = "张三"; zsClerk._gender = Gender.男; zsClerk._age = 22; zsClerk._department = "电气部"; zsClerk._workYears = 3; string name = "111"; //调用非静态方法 zsClerk.Write(); Clerk lsClerk = new Clerk(); lsClerk._name = "李四"; lsClerk._gender = Gender.男; lsClerk._age = 24; lsClerk._department = "软件部"; lsClerk._workYears = 2; name = "222"; lsClerk.Write(); Console.WriteLine(zsClerk._name);//字段 Console.WriteLine(lsClerk._name);//字段 Console.WriteLine(name); Console.WriteLine(name); //字段可以存放多个值,变量只能存放一个值 Console.ReadKey(); } } }