zoukankan      html  css  js  c++  java
  • 第二节 3属性 简单

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    /* 属性
     * 惯用法,属性开头字母大写,字段开头字母小写
     * 只用set或者只有get就可以定义只读或者只写属性(只写的不常见)
     * 可以为set get设置访问经别
     * (.Net3.x)简化set get; public int Age{ get; set; } 适合于set, get 中凤有特殊死搏斗逻辑代码的情况
     * 
     * 字段和属性的区别是什么?属性看似字段,不是字段,可以地非法值控制,可以设置只读
     * set get块内部其实就是get_****...
     */
    namespace _3属性
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*Person3 p = new Person3();
                p.Age = 22; //这里使用set方法
                Console.WriteLine("年龄是:{0}",p.Age);
                
                p.Age--; //这里进行的get操作
                Console.WriteLine("年龄是:{0}", p.Age);*/
    
                //Person4 p = new Person4();
                //p.Age++; //这里没什么用,操作++
                //Console.WriteLine(p.Age);//这里为3,但没有进行添加操作,因为没有保存
    
                Person5 p = new Person5();
                p.Age = 33;
                Console.WriteLine(p.Age);
                
                
                Console.ReadKey();
            }
        }
    
        class Person5 
        {
           private int age;
           public int Age 
           {
               set {
                   this.age = value;
               }
               get {
                   return this.age;
               }
           }
           
        }
    
        class Person3 
        {
            private int age;
            public int Age { //Age没有保存数据,都是保存到age中了
                set //赋值
                {
                    //这里对值进行过虑
                    if (value < 0) //public 字段和属性的区别,可以进行非法设置值的判断
                    {
                        return;
                    }
                    this.age = value;   //value就代表用户赋值过来的值 
                }
    
                get{ //取值
                    return this.age;
                }
            }
        }
    
        class Person4 
        {
            public int Age {
                set{ 
                }
    
                get{
                    return 3;
                }
            }
        }
    }
    

      

  • 相关阅读:
    成都的收藏品市场
    微信小程序 如何定义全局函数?
    Linux下 安装VMware Tools工具
    小程序圆角进度条实现方法
    Excel 将换行符替换为空
    再次学习mysql优化
    Subl 命令
    时间见证着—eternal life
    大巧不工web前端设计修炼之道—笔记
    批量更新某字段内容
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2365833.html
Copyright © 2011-2022 走看看