标签:new ble 它的 ted 类库 ext ase 派生类 protected
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assembly1 { public class BaseClass { protected internal int myValue = 0; internal int myValue2 = 0; } }
Assembly2:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Assembly1; //訪問Assembly1中public 類中的internal 成員和 protected internal 成員 namespace Assembly2 { /// <summary> /// 测试internal 在Assembly2程序集下声明一个internal类 /// </summary> internal class BaseClass2 { public static int intM = 0; } /// <summary> /// 測試private 声明两个私有成员 /// </summary> class Employee2 { private string name = "FirstName, LastName"; //兩個私有數據成員 private double salary = 100.0; public string GetName() { return name; } public double Salary { get { return salary; } } } /// <summary> /// 測試受保護成員protected 同一程序集下的 protectd internal 声明两个成员 /// </summary> class A { protected int x = 123; protected internal int myValue = 0; public int getx() { return x; } } //测试protected internal
class TestAccess2: BaseClass //继承自Assembly1程序集中的BaseClass类 测试不同程序集下访问protected internal { private void A() { TestAccess2 baseObject = new TestAccess2(); baseObject.myValue = 1; //myValue 為protected internal 雖然跨程序集但是由於TestAccess2 為派生類所以能夠訪問到 } } class TestAccess:A { static void Main(string[] args) {
//测试internal BaseClass mybase = new BaseClass(); //實例化另一個程序集中的BaseClass類 //mybase.myValue2 = 1; 报错 myValue2 為internal 在程序集Assembly1中 跨程序集Assembly2無法訪問 BaseClass2 mybase2 = new BaseClass2(); //因为BaseClass2在本程序集中所以能够实例化 //测试private Employee2 e = new Employee2(); // string n = e.name; // 报错,作為私有成員,只能通過成員方法來訪問 // double s = e.salary; string n = e.GetName(); double s = e.Salary; //测试protected A a = new A(); TestAccess b = new TestAccess(); //a.x = 10; 报错 x 為protected 只能通過派生類訪問 a.myValue = 10; //myValue屬性為protected internal: 只能在本类,派生类或者包含该类的程序集中访问 说明比protected范围更广 b.x = 10; //在派生类中才能訪問 } } }
public private internal protected protected internal 区别
标签:new ble 它的 ted 类库 ext ase 派生类 protected
原文地址:https://www.cnblogs.com/ygtup/p/9367431.html