English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Attributo (Property) Sono membri denominati (named) di classe (class), struttura (structure) e interfaccia (interface). I variabili di membro o i metodi di una classe o struttura sono chiamati Campo (Field). L'attributo (Property) è un'estensione del campo (Field) e può essere acceduto con la stessa sintassi. Utilizzano Accessori (accessors) Rende i valori dei campi privati leggibili, scrivibili o operabili.
L'attributo (Property) non determina la posizione di memorizzazione. Al contrario, hanno la capacità di leggere, scrivere o calcolare il loro valore. Accessori (accessors).
Ad esempio, c'è una classe chiamata Student con campi privati age, name e code. Non possiamo accedere direttamente a questi campi al di fuori della classe, ma possiamo avere attributi che accedono a questi campi privati.
Attributo (Property) delAccessore (accessor)Contiene espressioni eseguibili che consentono di ottenere (leggere o calcolare) o impostare (scrivere) attributi. La dichiarazione dell'accessore (accessor) può includere un accessore get, un accessore set, o entrambi. Ad esempio:
// 声明类型为 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; } }
Il seguente esempio dimostra l'uso delle proprietà (Property):
using System; namespace w3codebox { 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); // Aumentare l'età s.Age += 1; Console.WriteLine("Student Info: {0}", s); Console.ReadKey(); } } }
Quando il codice sopra viene compilato ed eseguito, produrrà i seguenti risultati:
Informazioni studente: Codice = 001, Nome = Zara, Età = 9 Informazioni studente: Codice = 001, Nome = Zara, Età = 10
Le classi astratte possono avere proprietà astratte, che devono essere implementate nelle classi derivate. Il seguente programma illustra questo punto:
using System; namespace w3codebox { 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("Informazioni studente:- {0}", s); // Aumentare l'età s.Age += 1; Console.WriteLine("Informazioni studente:- {0}", s); Console.ReadKey(); } } }
Quando il codice sopra viene compilato ed eseguito, produrrà i seguenti risultati:
Informazioni studente: Codice = 001, Nome = Zara, Età = 9 Informazioni studente: Codice = 001, Nome = Zara, Età = 10