工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> C言語

C# - プロパティ

プロパティ クラス、構造体、およびインターフェイスの名前付きメンバーです。クラスまたは構造体のメンバー変数またはメソッドは フィールド と呼ばれます .プロパティはフィールドの拡張であり、同じ構文を使用してアクセスされます。 アクセサを使用 これにより、プライベート フィールドの値を読み取り、書き込み、または操作できます。

プロパティは、保存場所に名前を付けません。代わりに、アクセサーがあります それらの値を読み取り、書き込み、または計算します。

たとえば、年齢、名前、コードのプライベート フィールドを持つ Student という名前のクラスがあるとします。クラス スコープ外からこれらのフィールドに直接アクセスすることはできませんが、これらのプライベート フィールドにアクセスするためのプロパティを設定することはできます。

アクセサー

アクセサー プロパティの には、プロパティの取得 (読み取りまたは計算) または設定 (書き込み) に役立つ実行可能ステートメントが含まれています。アクセサー宣言には、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 tutorialspoint {
   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();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます −

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

抽象プロパティ

抽象クラスには、派生クラスで実装する必要がある抽象プロパティがある場合があります。次のプログラムはこれを示しています −

ライブデモ
using System;

namespace tutorialspoint {
   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();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます −

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

C言語

  1. 銀の抗菌特性
  2. 算術特性
  3. 指数のプロパティ
  4. モリブデン-ハフニウム-カーボン (MHC)
  5. AMPCO® 25 鍛造品
  6. UGICHROM® 4462
  7. UGICHROM® 4362
  8. バイニデュール® 1300
  9. バイニデュール® 7980 CN
  10. UGICHROM® 4460
  11. Printdur® 4404