산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> 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 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. Bainidur® 7980 CN
  10. UGICHROM® 4460