简述 private、protected、internal、public、protected internal 修饰符的访问权限

时间:2022-03-13 15:11:15

private:只能在本类中使用。
internal:同一程序集中的类可以使用。
public:所有类均可使用。
protected:在本类中及其子类中可以使用。
protected internal: 当前程序集中和它的子类才能访问它。
简述 private、protected、internal、public、protected internal 修饰符的访问权限
Project X:

简述 private、protected、internal、public、protected internal 修饰符的访问权限简述 private、protected、internal、public、protected internal 修饰符的访问权限View Code
public class A
{
// private:只能在本类中使用
private int i = 0;

// internal:同一命名空间(程序集)中的类可以使用
internal int j = 1;

// public:所有类均可使用
public int k = 2;

// protected:在本类中及其子类中可以使用
protected int l = 3;

// protected internal: 当前程序集中和它的子类才能访问它
protected internal int m = 4;

public void TestPrivate()
{
i
= 0;
}
}

public class B
{
public void TestInternal()
{
A a
= new A();
a.j
= 1;
}

public void TestPublic()
{
A a
= new A();
a.k
= 2;
}

public void TestProInt()
{
A a
= new A();
a.m
= 4;
}
}

public class C : A
{
public void TestInternal()
{
A a
= new A();
a.j
= 1;

C c
= new C();
c.j
= 1;
}

public void TestPublic()
{
A a
= new A();
a.k
= 2;

C c
= new C();
c.k
= 2;
}

public void TestProtected()
{
C c
= new C();
c.l
= 3;
}

public void TestProInt()
{
A a
= new A();
a.m
= 4;

C c
= new C();
c.m
= 4;
}
}

class MainClass
{
static void Main()
{

}
}

Project Y:

简述 private、protected、internal、public、protected internal 修饰符的访问权限简述 private、protected、internal、public、protected internal 修饰符的访问权限View Code
public class D : A
{
public void TestPublic()
{
A a
= new A();
a.k
= 2;

D d
= new D();
d.k
= 2;
}

public void TestProtected()
{
D d
= new D();
d.l
= 3;
}

public void TestProInt()
{
D d
= new D();
d.m
= 4;
}
}

public class E
{
public void TestPublic()
{
A a
= new A();
a.k
= 2;
}
}