Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phạm vi của biến thành viên protected trong C#

Protected cho phép child class truy cập member variables/functions của base class. Hỗ trợ implement inheritance.

Ví dụ set protected member trong Class A:

class A { protected int a2 = 87; }

Truy cập từ derived class object:

Ví dụ

using System;
class A { protected int a2 = 87; }
class B : A {
    static void Main() {
        A a = new A(); B b = new B();
        b.a2 = 10; // OK: derived class truy cập protected member
    }
}