using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { student s = new student();//初始化对象 //s.Code = 1; s.Name = "zhangsan";//Code,Name是类的成员 s.shuzi = 19; Console.Write(s.Code.ToString()); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class student { private int _Code;//private只能在自己的类中访问 public int Code//封装属性 {//有两个方法 get { return shuzi; }//1.读 set { if (value >= 1 && value <= 10) _Code = value; else _Code = 0; }//2.取 } public int shuzi; public string Name;//public可以再其他类中访问 } }