Thuộc tính (Property) trong C | 85 bài học lập trình C hay nhất

8 176 0
Thuộc tính (Property) trong C  | 85 bài học lập trình C  hay nhất

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Thuộc tính (Property) C# Thuộc tính - Property thành viên đặt tên lớp, cấu trúc, Interface Các biến thành viên phương thức lớp cấu trúc gọi Field Thuộc tính kế thừa Field truy cập sử dụng cú pháp Chúng sử dụng accessor thơng qua giá trị Private Field đọc, viết thao tác Thuộc tính (Property) khơng đặt tên vị trí lưu giữ Thay vào đó, chúng có accessors mà đọc, ghi tính tốn giá trị chúng Ví dụ, có lớp với tên Student, với Private Field cho age, name, code Chúng ta trực tiếp truy cập Field từ bên phạm vi lớp đó, có thuộc tính để truy cập Private Field Accessor C# Trong C#, accessor thuộc tính chứa lệnh thực thi, mà giúp đỡ việclấy (đọc tính tốn) thiết lập (ghi) thuộc tính Các khai báo accessor thu get accessor, set accessor, hai Ví dụ: // 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 http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com { get { return name; } set { name = value; } } // Declare a Age property of type int: public int Age { get { return age; } set { age = value; } } Ví dụ Ví dụ minh họa cách sử dụng thuộc tính C#: using System; namespace tutorialspoint { class Student { private string code = "N.A"; private string name = "not known"; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com 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 { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com 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(); } } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com } Khi code biên dịch thực thi, cho kết quả: Student Info: Code = 001, Name = Zara, Age = Student Info: Code = 001, Name = Zara, Age = 10 Thuộc tính trừu tượng C# Một lớp Abstract có thuộc tính abstract, mà nên triển khai lớp kế thừa Chương trình sau minh họa điều này: 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; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com // 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; } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com 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(); } } } Khi code biên dịch thực thi, cho kết quả: http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Student Info: Code = 001, Name = Zara, Age = Student Info: Code = 001, Name = Zara, Age = 10 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... vietjack.com } Khi code biên dịch th c thi, cho kết quả: Student Info: Code = 001, Name = Zara, Age = Student Info: Code = 001, Name = Zara, Age = 10 Thu c tính trừu tượng C# Một lớp Abstract c thu c. .. họa c ch sử dụng thu c tính C# : using System; namespace tutorialspoint { class Student { private string code = "N.A"; private string name = "not known"; http://vietjack.com/ Trang chia sẻ h c online... http://vietjack.com/csharp/index.jsp Copyright © vietjack.com private int age = 0; // Declare a Code property of type string: public string Code { get { return code; } set { code = value; } } // Declare

Ngày đăng: 02/12/2017, 20:01

Mục lục

    Thuộc tính (Property) trong C#

    Thuộc tính trừu tượng trong C#

Tài liệu cùng người dùng

Tài liệu liên quan