面向对象 封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class student
    {
        private int _code;

        public int Code//封装 属性 属性是某一个成员变量的附属品 他是为某一个成员变量传递 而不是自己存东西 只有变量才自己存东西
        {
           /*get { return _code; }//读
            set {
                if (value >= 1 && value <= 10)
                    _code = value;

                else
                    _code = 0;*/
            get { return shuzi; }//读
            set
            {
                if (value >= 1 && value <= 10)
                    _code = value;

                else
                    _code = 0;
            }//写
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program  //类  用户自定义数据类型
    {
        static void Main(string[] args)
        {
            student s = new student(); //new初始化对象 s 对象

            //s.code = 1;    // code,name是类的成员变量
            s.name = "zhangsan";
            //s.Code = 1;
            s.shuzi = 11;
            Console.Write(s.Code .ToString());

            //Console.Write(s.name);

            //Console.Write("hello");

            Console.ReadLine();

        }
    }
}

  

} //取值走get 给code赋值的时候走set public int shuzi; public string name; } }

  

原文地址:https://www.cnblogs.com/zhuxu/p/4939708.html