zoukankan      html  css  js  c++  java
  • C#中属性的解析

    一.域的概念

    C#中域是指成员变量和方法,在OOP编程中(面向对象编程)我们要求用户只知道类是干什么的,而不许知道如何完成的,或者说不允许访问类的内部,对于有必要在类外可见的域,我们用属性来表达,所以说属性可以看成是域的扩展。我们通过操作访问器来操作属性,进而达到对私有域的操作。

    二.访问器

    属性的访问器包含有助于获取(读取或计算) 或设置(写入)属性的可执行语句,访问器申明可以包含一个get访问器,一个set访问器,或者同时包含两者。例如:

    // 声明类型为 string 的 Code 属性
    public string Code
    {
       get
       {
          return code;
       }
       set
       {
          code = value;
       }
    }
    
    // 声明类型为 string 的 Name 属性
    public string Name
    {
       get
       {
         return name;
       }
       set
       {
         name = value;
       }
    }
    
    // 声明类型为 int 的 Age 属性
    public int Age
    { 
       get
       {
          return age;
       }
       set
       {
          age = value;
       }
    }

    实例

    下面的实例展示了属性的用法:

    using System;
    namespace runoob
    {
       class Student
       {
    
          private string code = "N.A";
          private string name = "not known";
          private int age = 0;
    
          // 声明类型为 string 的 Code 属性
          public string Code
          {
             get
             {
                return code;
             }
             set
             {
                code = value;
             }
          }
       
          // 声明类型为 string 的 Name 属性
          public string Name
          {
             get
             {
                return name;
             }
             set
             {
                name = value;
             }
          }
    
          // 声明类型为 int 的 Age 属性
          public int Age
          {
             get
             {
                return age;
             }
             set
             {
                age = value;
             }
          }
          public override string ToString()
          {
             return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
          }
        }
        class ExampleDemo
        {
          public static void Main()
          {
             // 创建一个新的 Student 对象
             Student s = new Student();
                
             // 设置 student 的 code、name 和 age
             s.Code = "001";
             s.Name = "Zara";
             s.Age = 9;
             Console.WriteLine("Student Info: {0}", s);
             // 增加年龄
             s.Age += 1;
             Console.WriteLine("Student Info: {0}", s);
             Console.ReadKey();
           }
       }
    }

    三.抽象属性

    抽象类可以拥有抽象属性,在子类中被实现,下面程序说明了这点:

    using System;
    namespace runoob
    {
       public abstract class Person
       {
          public abstract string Name
          {
             get;
             set;
          }
          public abstract int Age
          {
             get;
             set;
          }
       }
       class Student : Person
       {
    
          private string code = "N.A";
          private string name = "N.A";
          private int age = 0;
    
          // 声明类型为 string 的 Code 属性
          public string Code
          {
             get
             {
                return code;
             }
             set
             {
                code = value;
             }
          }
       
          // 声明类型为 string 的 Name 属性
          public override string Name
          {
             get
             {
                return name;
             }
             set
             {
                name = value;
             }
          }
    
          // 声明类型为 int 的 Age 属性
          public override int Age
          {
             get
             {
                return age;
             }
             set
             {
                age = value;
             }
          }
          public override string ToString()
          {
             return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
          }
       }
       class ExampleDemo
       {
          public static void Main()
          {
             // 创建一个新的 Student 对象
             Student s = new Student();
                
             // 设置 student 的 code、name 和 age
             s.Code = "001";
             s.Name = "Zara";
             s.Age = 9;
             Console.WriteLine("Student Info:- {0}", s);
             // 增加年龄
             s.Age += 1;
             Console.WriteLine("Student Info:- {0}", s);
             Console.ReadKey();
           }
       }
    }

    四.自动属性

    从C#3.0开始,C#引入了自动属性的概念,以下两种代码是等效的:

    public class Student
    {
        public string Name { get; set; }
    }
    public class Student
    {
        private string name; // This is the so-called "backing field"
        public string Name // This is your property
        {
            get {return name;}
            set {name = value;}
        }
    }
  • 相关阅读:
    nodejs程序发布的jenkins自动化脚本和问题记录
    jenkins代码rsync推送脚本带日志记录和代码分机房处理示例
    django入门到精通⑥消息管理器的升级处理,对关键词进行过滤示例
    django入门到精通⑤mako模板的使用
    django入门到精通④jinja2模板的使用
    django入门到精通③template模板功能和常用标签过滤器的使用
    hdfs副本调整不生效
    macOS 系统中,开发工具列表
    查看系统中安装了哪些python版本
    -p py, --python py target interpreter for which to create a virtual (either absolute path or identifier string) 中 identifier string 的含义
  • 原文地址:https://www.cnblogs.com/Artist007/p/11295442.html
Copyright © 2011-2022 走看看