Nền tảng Kiến thức - Hành trang tới Tương lai
Card image

Chương trình học


  1. Cài đặt môi trường Lập trình C# 2
    1. Cài đặt Visual Studio
    2. Môi trường phát triển .NET
  2. Nhập môn Lập trình C# 18
    1. Giới thiệu ngôn ngữ lập trình C#
    2. Cấu trúc chương trình C#
    3. Cú pháp cơ bản C#
    4. Các kiểu dữ liệu trong C#
    5. Chuyển đổi kiểu dữ liệu trong C#
    6. Khởi tạo biến trong C#
    7. Hằng số trong C#
    8. Toán tử trong C#
    9. Điều kiện trong C#
    10. Vòng lặp trong C#
    11. Tính bao đóng trong C#
    12. Tạo phương thức/hàm trong C#
    13. Đối tượng Nullable trong C#
    14. Mảng trong C#
    15. Chuỗi trong C#
    16. Cấu trúc trong C#
    17. Enums trong C#
    18. Truyền Tham số Reference hay Tham trị (Value) trong C#
  3. Hướng đối tượng trong C# 11
    1. Class trong C#
    2. Kế thừa trong C#
    3. Tính đa hình trong C#
    4. Nạp chồng toán tử trong C#
    5. Giao diện (Interface) trong C#
    6. Namespace trong C#
    7. Các lệnh tiền xử lý trong C#
    8. Biểu thức chính quy (Regular) trong C#
    9. Bắt các lỗi/ngoại lệ (Exception) trong C#
    10. Xử lý Đọc/Ghi File trong C#
    11. LINQ trong C#
  4. Các kỹ thuật nâng cao trong C# 2
    1. Thuộc tính (Attributes) trong C#
    2. Biên dịch ngược (Reflection) trong C#
  5. Bài tập thực hành 27
    1. Khai báo các Kiểu dữ liệu cho Mẫu Lý lịch A2 và Mẫu Hóa đơn Bán hàng
    2. Sử dụng các Toán tử cơ bản trong C#
    3. Kiểm tra số chẵn hay lẻ
    4. Thay đổi vị trí của 2 phần tử
    5. Tính tổng các kí tự số
    6. Đảo ngược con số
    7. Tạo chương trình ATM đơn giản
    8. Tạo chương trình ATM đơn giản với các phương án rút tiền theo các mệnh giá
    9. Tìm số Max, Min trong mảng 2 chiều
    10. Tạo cấu trúc lưu trữ thông tin Nhân viên
    11. Làm quen Hướng đối tượng trong C#
    12. Mã hóa chuỗi với Hacker Speak (H4ck3rSp34k)
    13. Mã hóa chuỗi với Alternating Captions (AlTeRnAtInG_CaPs​​​​​)
    14. Tính tổng 2 số nhỏ nhất trong danh sách
    15. Trích xuất thông tin từ dữ liệu trong FILE TEXT
    16. In bảng cửu chương
    17. In tam giác Nhị phân
    18. In tam giác Số ký tự
    19. Đếm số 1
    20. Sử dụng Mảng 2 chiều để in tên dạng Asterisk ra màn hình
    21. Sử dụng Mảng 1 chiều để phân tách Tên với khoảng cách
    22. Bài tập Biểu thức Chính quy (Regular Expression)
    23. Ghi log lỗi với File và Try Catch
    24. Ghi Access log
    25. LINQ group by tên tập tin
    26. LINQ với collection
    27. Tạo chương trình Quản lý Danh sách Sinh viên và Giảng viên
  6. Kiểm tra kiến thức 1
    1. Kiểm tra kiến thức Lập trình C#
  7. Kiểm tra kiến thức - Đồ án 2
    1. Bài tập Kiểm tra Thực hành C# - Đề 01
    2. Bài tập Kiểm tra Thực hành C# - Đề 02

Chương 2-Bài 11. Tính bao đóng trong C#

Tác giả: Dương Nguyễn Phú Cường
Ngày đăng: Hồi xưa đó

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details. Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction. Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers −
  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

Public Access Specifier

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class. The following example illustrates this −
using System;

namespace RectangleApplication {
   class Rectangle {
      //member variables
      public double length;
      public double width;
      
      public double GetArea() {
         return length * width;
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle
   
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.length = 4.5;
         r.width = 3.5;
         r.Display();
         Console.ReadLine();
      }
   }
}
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75
In the preceding example, the member variables length and width are declared public, so they can be accessed from the function Main() using an instance of the Rectangle class, named r. The member function Display() and GetArea() can also access these variables directly without using any instance of the class. The member functions Display() is also declared public, so it can also be accessed from Main() using an instance of the Rectangle class, named r.

Private Access Specifier

Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. The following example illustrates this −
using System;

namespace RectangleApplication {
   class Rectangle {
      //member variables
      private double length;
      private double width;
      
      public void Acceptdetails() {
         Console.WriteLine("Enter Length: ");
         length = Convert.ToDouble(Console.ReadLine());
         Console.WriteLine("Enter Width: ");
         width = Convert.ToDouble(Console.ReadLine());
      }
      public double GetArea() {
         return length * width;
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle
   
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.Acceptdetails();
         r.Display();
         Console.ReadLine();
      }
   }
}
When the above code is compiled and executed, it produces the following result −
Enter Length:
4.4
Enter Width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52
In the preceding example, the member variables length and width are declared private, so they cannot be accessed from the function Main(). The member functions AcceptDetails() and Display() can access these variables. Since the member functions AcceptDetails() and Display() are declared public, they can be accessed from Main() using an instance of the Rectangle class, named r.

Protected Access Specifier

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.

Internal Access Specifier

Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined. The following program illustrates this −
using System;
namespace RectangleApplication {
   class Rectangle {
      //member variables
      internal double length;
      internal double width;
      
      double GetArea() {
         return length * width;
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle
   
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.length = 4.5;
         r.width = 3.5;
         r.Display();
         Console.ReadLine();
      }
   }
}
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75
In the preceding example, notice that the member function GetArea() is not declared with any access specifier. Then what would be the default access specifier of a class member if we don't mention any? It is private.

Protected Internal Access Specifier

The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.

Các bài học

Chương trình học

Bao gồm Module, Chương, Bài học, Bài tập, Kiểm tra...

Chương trình học


  1. Cài đặt môi trường Lập trình C# 2
    1. Cài đặt Visual Studio
    2. Môi trường phát triển .NET
  2. Nhập môn Lập trình C# 18
    1. Giới thiệu ngôn ngữ lập trình C#
    2. Cấu trúc chương trình C#
    3. Cú pháp cơ bản C#
    4. Các kiểu dữ liệu trong C#
    5. Chuyển đổi kiểu dữ liệu trong C#
    6. Khởi tạo biến trong C#
    7. Hằng số trong C#
    8. Toán tử trong C#
    9. Điều kiện trong C#
    10. Vòng lặp trong C#
    11. Tính bao đóng trong C#
    12. Tạo phương thức/hàm trong C#
    13. Đối tượng Nullable trong C#
    14. Mảng trong C#
    15. Chuỗi trong C#
    16. Cấu trúc trong C#
    17. Enums trong C#
    18. Truyền Tham số Reference hay Tham trị (Value) trong C#
  3. Hướng đối tượng trong C# 11
    1. Class trong C#
    2. Kế thừa trong C#
    3. Tính đa hình trong C#
    4. Nạp chồng toán tử trong C#
    5. Giao diện (Interface) trong C#
    6. Namespace trong C#
    7. Các lệnh tiền xử lý trong C#
    8. Biểu thức chính quy (Regular) trong C#
    9. Bắt các lỗi/ngoại lệ (Exception) trong C#
    10. Xử lý Đọc/Ghi File trong C#
    11. LINQ trong C#
  4. Các kỹ thuật nâng cao trong C# 2
    1. Thuộc tính (Attributes) trong C#
    2. Biên dịch ngược (Reflection) trong C#
  5. Bài tập thực hành 27
    1. Khai báo các Kiểu dữ liệu cho Mẫu Lý lịch A2 và Mẫu Hóa đơn Bán hàng
    2. Sử dụng các Toán tử cơ bản trong C#
    3. Kiểm tra số chẵn hay lẻ
    4. Thay đổi vị trí của 2 phần tử
    5. Tính tổng các kí tự số
    6. Đảo ngược con số
    7. Tạo chương trình ATM đơn giản
    8. Tạo chương trình ATM đơn giản với các phương án rút tiền theo các mệnh giá
    9. Tìm số Max, Min trong mảng 2 chiều
    10. Tạo cấu trúc lưu trữ thông tin Nhân viên
    11. Làm quen Hướng đối tượng trong C#
    12. Mã hóa chuỗi với Hacker Speak (H4ck3rSp34k)
    13. Mã hóa chuỗi với Alternating Captions (AlTeRnAtInG_CaPs​​​​​)
    14. Tính tổng 2 số nhỏ nhất trong danh sách
    15. Trích xuất thông tin từ dữ liệu trong FILE TEXT
    16. In bảng cửu chương
    17. In tam giác Nhị phân
    18. In tam giác Số ký tự
    19. Đếm số 1
    20. Sử dụng Mảng 2 chiều để in tên dạng Asterisk ra màn hình
    21. Sử dụng Mảng 1 chiều để phân tách Tên với khoảng cách
    22. Bài tập Biểu thức Chính quy (Regular Expression)
    23. Ghi log lỗi với File và Try Catch
    24. Ghi Access log
    25. LINQ group by tên tập tin
    26. LINQ với collection
    27. Tạo chương trình Quản lý Danh sách Sinh viên và Giảng viên
  6. Kiểm tra kiến thức 1
    1. Kiểm tra kiến thức Lập trình C#
  7. Kiểm tra kiến thức - Đồ án 2
    1. Bài tập Kiểm tra Thực hành C# - Đề 01
    2. Bài tập Kiểm tra Thực hành C# - Đề 02

Bài học trước Bài học tiếp theo