C#类属性

C#类属性 首页 / C#入门教程 / C#类属性

属性是类、结构和接口的命名成员,类或结构中的成员变量或方法称为字段,属性是字段的扩展,使用相同的语法访问,它们使用访问器,通过它可以读取、写入或操作私有字段的值。

访问器

属性的访问器包含帮助获取或设置属性的可执行语句,访问器声明可以包含get访问器和/或set访问器。例如-

//Declare a Code property of type string:
public string Code {
   get {
      return code;
   }
   set {
      code = value;
   }
}

//Declare a Name property of type string:
public string Name {
   get {
      return name;
   }
   set {
      name = value;
   }
}

//Declare a Age property of type int:
public int Age { 
   get {
      return age;
   }
   set {
      age = value;
   }
}

下面的示例演示属性的用法

using System;
namespace learnfk {
   class Student {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;
      
      //Declare a Code property of type string:
      public string Code {
         get {
            return code;
         }
         set {
            code = value;
         }
      }
      
      //Declare a Name property of type string:
      public string Name {
         get {
            return name;
         }
         set {
            name = value;
         }
      }
      
      //Declare a Age property of type int:
      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() {
      
         //Create a new Student object:
         Student s = new Student();
         
         //Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
         
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-properties.html

来源:LearnFk无涯教程网

Student Info: Code=001, Name=Zara, Age=9
Student Info: Code=001, Name=Zara, Age=10

抽象属性

抽象类可以具有抽象属性,该属性应该在派生类中实现。以下程序说明了此-

using System;

namespace learnfk {
   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;
      
      //Declare a Code property of type string:
      public string Code {
         get {
            return code;
         }
         set {
            code = value;
         }
      }
      
      //Declare a Name property of type string:
      public override string Name {
         get {
            return name;
         }
         set {
            name = value;
         }
      }
      
      //Declare a Age property of type int:
      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() {
         //Create a new Student object:
         Student s = new Student();
         
         //Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info:- {0}", s);
         
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info:- {0}", s);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-properties.html

来源:LearnFk无涯教程网

Student Info: Code=001, Name=Zara, Age=9
Student Info: Code=001, Name=Zara, Age=10

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

AI大模型企业应用实战 -〔蔡超〕

React Native 新架构实战课 -〔蒋宏伟〕

玩转Vue 3全家桶 -〔大圣〕

数据分析思维课 -〔郭炜〕

如何看懂一幅画 -〔罗桂霞〕

ZooKeeper实战与源码剖析 -〔么敬国〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

从0开始做增长 -〔刘津〕

微服务架构核心20讲 -〔杨波〕

好记忆不如烂笔头。留下您的足迹吧 :)