class App
{
private string _args;
public string Args
{
get { return _args; }
set { _args = value; }
}
public void Run()
{
//Write Args
}
}后来我们将配置信息移到另一个类AppStartInfo中:
class App
{
public void Run(AppStartInfo startInfo)
{
//Write startInfo.Args
}
}
class AppStartInfo
{
private string _args;
public string Args
{
get { return _args; }
set { _args = value; }
}
public AppStartInfo()
{ }
public AppStartInfo(string args)
{
_args = args;
}
}AppStartInfo和Web应用程序多层结构中的实体类非常相似,后来又想这样没什么意思,两个明明相关的两个类却被弄成毫无关系,希望App有个AppStartInfo类型的属性:
class App
{
private AppStartInfo _startInfo = null;
public AppStartInfo StartInfo
{
get
{
if (_startInfo == null)
_startInfo = new AppStartInfo();
return _startInfo;
}
set
{
if (value is AppStartInfo)
_startInfo = value;
else
throw new ArgumentException("StartInfo赋值非法!");
}
}
public void Run()
{
//Write StartInfo.Args
}
}调用示例:
AppStartInfo startInfo = new AppStartInfo("Mzwu.Com");
App app = new App();
app.StartInfo = startInfo;
app.Run();后来发现还有其他的赋值方法:
App app = new App();
app.StartInfo.Args = "Hello World!";
app.Run();这样AppStartInfo就完全是为App服务的,相当于是App的附属类,想想System.Diagnostics.Process对象的StartInfo属性,是不是也是这样子的?
评论(0)
暂无评论。