Introduction to access modifiers such as C # Basics

Source: Internet
Author: User

1. Access modifiers of members in the class

The location modifier is the region that the member can access (use. In C #, the following modifiers are commonly used: pubic (public), private (private), internal (Inline), and protected (protected ). The Restricted Area of each modifier is illustrated.

Copy codeThe Code is as follows: class TestClass
{
Public int a = 0;
Private int B = 0;
Protected int c = 0;

Pulic static int d = 0;
}

In the TestClass class, variable a is of the Public type and can be in the out-of-class orientation, that is, Class a can be used after instantiation; variable B can only be accessed within the class, that is, the function inside the class can use B; the variable c is available for the TestClass inheritance class.Copy codeThe Code is as follows: class B
{
Private void st ()
{
TestClass tec = new TestClass ();
Int aa = tec. a; // right
Int bb = tec. B; // wrong
Int cc = tec. c // wrong
}

TestClass instantiate the object tec. tec can access a but cannot access B and c. Under what circumstances can access B and c?Copy codeThe Code is as follows: class C: TestClass
{
Private void st ()
{
TestClass tec = new TestClass ();
C bo = new C ();
Int aa = tec.;
Int bb = tec. B; // wrong
Int cc = tec. c; // wrong
Int tt = bo. c;
}
}

C first. C is a protected type, and its inherited Class C can be accessed. The Class B instantiated object is still inaccessible. But B can access c internally. As shown below, there are no restrictions in the category class.Copy codeThe Code is as follows: class TestClass
{
Public int a = 0;
Private int B = 0;
Protected int c = 0;

Pulic static int d = 0;
Private void OS ()
{
Int a1 =;
Int a2 = B;
Int a3 = c;
}
}

After B is inherited, c becomes private. That is to say, B's inheritance class cannot access c; as shown below, D cannot access c.Copy codeThe Code is as follows: class D: B
{
Private void st ()
{
B bo = new B ();
Int dd = bo. c; // restricted access to wrong
}
}

The static modifier indicates that the field or method belongs to the class, not a specific object. When the TestClass class is instantiated, the values of a, B, and c are different, but d is the same. If a, B, and c are equivalent to the province in which everyone is located, d indicates that everyone is in China.Copy codeThe Code is as follows: class B: TestClass
{
TestClass tec = new TestClass ();
Int bb = tec. a; // wrong error cause: the initial value of the field references non-static fields.
Int cc = TestClass. d;
Private void st ()
{

B bo = new B ();
Int aa = tec.;
Int tt = bo. c;
}
}

Summary:

Puclic: Class, both inside and outside the class; private: Internal; protect: class, that is, its derived class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.