[英]Protecting Base class object in C#
怎么樣
public class BaseAppConstants
{
public const string StLiveIdCookieName = "XYZ";
}
public class AppConstants : BaseAppConstants
{
}
我該如何進行更改,使用戶無法直接訪問他們只能使用的基類,例如AppConstants.StLiveIdCookieName;
你應該
A)將常量移到應該使用的級別(即在AppConstants
聲明並將其從BaseAppConstants
刪除),或者,
B)使用不同的修飾符使其無法訪問,並在其他類中提供訪問器(即在BaseAppConstants
使用protected
的BaseAppConstants
並在AppConstants
使用諸如public const string StLiveIdCookieName = BaseAppConstants.StLiveIdCookieName
重新實現-但這種方式違背了常量的用法) 。
如果將基類中的屬性設置為protected,則該屬性僅在派生類中可用。
public class BaseAppConstants
{
protected const string StLiveIdCookieName = "XYZ";
}
在此處閱讀有關受保護的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.