我有两个类
第 1 类。
public class BaseContentPage : System.Web.UI.Page
{
}
第 2 类。
public class BaseUserControl : System.Web.UI.UserControl
{
}
现在我希望他们知道这个类(class)。
public class BaseCommon
{
public string Variable1 { get; set; }
public string Variable2 { get; set; }
public string Variable3 { get; set; }
}
我目前的做法是像这样将 BaseCommon 类的变量设为静态
public static string Variable1 { get; set; }
public static string Variable2 { get; set; }
public static string Variable3 { get; set; }
我还能怎么做?
最佳答案
使用组合。
为 BaseContentPage 和 BaseUserControl 提供一个 BaseCommon 类型的私有(private)字段(如果需要可以将其作为属性公开)(不要将其设为静态)。
他们可以创建 BaseCommon 或通过构造函数(依赖注入(inject))传入一个实例。
关于c# - 是否有可能有 2 个基类已经继承自某个继承或知道第三个公共(public)类的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1563804/