C# 如何在方法运行之前使用派生 class 变量设置基 class 变量
C# How to set a base class variable using a derived class variable before a method runs
这里只问了第二个问题,所以如果我遗漏了一些有助于评论的代码。
我有一个摘要 class 报告,一个名为 BusinessBreakdown 的派生 class 和一个调用 BusinessWrittenBreakdown 的派生 class。
如何更改此代码,以便 BusinessWrittenBreakdown 中的 ReportTitle、DateField 和 DataLabel 在运行 BuildDefinition 方法之前在 BusinessBreakdown 中设置同名的构造函数。
我已经尝试使用谷歌搜索,但无法弄清楚如何,这是我所能得到的。出现的错误似乎无济于事,所以我更想寻求一种可行的不同方法。
public class BusinessBreakdown : Report
{
static string ReportTitle;
static string DateField;
static string DateLabel;
public BusinessBreakdown(string theReportTitle, string theDateField, string theDateLabel)
: base(BuildDefinition)
{
ReportTitle = theReportTitle;
DateField = theDateField;
DateLabel = theDateLabel;
}
/// <summary>
/// Build the report definition
/// </summary>
/// <returns></returns>
public static ReportDef BuildDefinition(Settings settings)
{
// Create definition
var rdef = new ReportDef();
// Create configuration context
var context = new FsConfigContext();
rdef.ConfigContext = context;
// Report title
rdef.ReportTitle = ReportTitle;
// Create report date range configuration
ConfigDateRange drange = new ConfigDateRange(settings, "ReportDate", Config.ConfigDisposition.Filter,
new FilterExpressionDef
{
Expression = DateField
}, DateLabel);
rdef.ReportDate = drange;
///// code ...
return rdef;
}
}
}
public class BusinessWrittenBreakdown : BusinessBreakdown
{
// Report title
static string ReportTitle = "Business Written Breakdown Report";
// Report date range and label
static string DateField = "COMMISS.BRDateWritten";
static string DateLabel = "Written Date";
public BusinessWrittenBreakdown()
: base(ReportTitle, DateField, DateLabel)
{
}
/// more code...
}
}
你绝对无法控制那个顺序。这是一个static
方法。
public static ReportDef BuildDefinition(Settings settings)
根据定义,有人可以在 从未 调用任一 class 的构造函数的情况下调用此方法。所以没办法让他们先调用构造函数。
静态属性和方法在 class 的所有实例之间共享。你有一个具有这些不同属性的构造函数表明你想为一个报告创建一个 class 的实例,然后为另一个报告创建另一个具有不同参数的实例。
在这种情况下,您似乎不需要静态方法,它们只会增加复杂性。所以我只想删除 static
。这意味着每个 属性 或字段都是一个 "instance" 属性 或字段。它属于 class 的每个单独实例,而不是在 class 的实例之间共享。
这里只问了第二个问题,所以如果我遗漏了一些有助于评论的代码。
我有一个摘要 class 报告,一个名为 BusinessBreakdown 的派生 class 和一个调用 BusinessWrittenBreakdown 的派生 class。
如何更改此代码,以便 BusinessWrittenBreakdown 中的 ReportTitle、DateField 和 DataLabel 在运行 BuildDefinition 方法之前在 BusinessBreakdown 中设置同名的构造函数。
我已经尝试使用谷歌搜索,但无法弄清楚如何,这是我所能得到的。出现的错误似乎无济于事,所以我更想寻求一种可行的不同方法。
public class BusinessBreakdown : Report
{
static string ReportTitle;
static string DateField;
static string DateLabel;
public BusinessBreakdown(string theReportTitle, string theDateField, string theDateLabel)
: base(BuildDefinition)
{
ReportTitle = theReportTitle;
DateField = theDateField;
DateLabel = theDateLabel;
}
/// <summary>
/// Build the report definition
/// </summary>
/// <returns></returns>
public static ReportDef BuildDefinition(Settings settings)
{
// Create definition
var rdef = new ReportDef();
// Create configuration context
var context = new FsConfigContext();
rdef.ConfigContext = context;
// Report title
rdef.ReportTitle = ReportTitle;
// Create report date range configuration
ConfigDateRange drange = new ConfigDateRange(settings, "ReportDate", Config.ConfigDisposition.Filter,
new FilterExpressionDef
{
Expression = DateField
}, DateLabel);
rdef.ReportDate = drange;
///// code ...
return rdef;
}
}
}
public class BusinessWrittenBreakdown : BusinessBreakdown
{
// Report title
static string ReportTitle = "Business Written Breakdown Report";
// Report date range and label
static string DateField = "COMMISS.BRDateWritten";
static string DateLabel = "Written Date";
public BusinessWrittenBreakdown()
: base(ReportTitle, DateField, DateLabel)
{
}
/// more code...
}
}
你绝对无法控制那个顺序。这是一个static
方法。
public static ReportDef BuildDefinition(Settings settings)
根据定义,有人可以在 从未 调用任一 class 的构造函数的情况下调用此方法。所以没办法让他们先调用构造函数。
静态属性和方法在 class 的所有实例之间共享。你有一个具有这些不同属性的构造函数表明你想为一个报告创建一个 class 的实例,然后为另一个报告创建另一个具有不同参数的实例。
在这种情况下,您似乎不需要静态方法,它们只会增加复杂性。所以我只想删除 static
。这意味着每个 属性 或字段都是一个 "instance" 属性 或字段。它属于 class 的每个单独实例,而不是在 class 的实例之间共享。