构造函数在C#中触发继承 [英] Constructor firing Inheritance in C#

查看:87
本文介绍了构造函数在C#中触发继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,这是一个代码的简化示例,

演示了它:

公共类BizObj

{

public string TableName ="" ;;

private DataSet oData;


public BizObj()

{

LoadData();

}


public void LoadData()

{

SqlConnection oConn = new SqlConnection(" ...");

SqlDataAdapter oAdapter = new SqlDataAdapter(" SELECT * FROM

" + this.TableName,oConn);

oAdapter.Fill(oData);


}

}


公共类AuthorBizObj:BizObj

{

public AuthorBizObj()

{

this.TableName =" Authors";

}

}


现在,因为基类构造函数首先触发,在TableName可以设置为作者之前调用LoadData




所以我尝试覆盖TableName字段的方式如下,希望

它将在构造函数方法触发之前设置:

public class AuthorBizObj:BizObj

{

public string TableName =" Authors";


public AuthorBizObj()

{

}

}


但我得到一个编译警告:

AuthorBizObj.TableName需要关键字new,因为它隐藏继承成员BizObj.TableName。


我不是想隐藏成员,我试图覆盖它。


如何确保TableName字段的值为Authors?之前

BizObj构造函数会触发吗?


< b>这个问题严格来说是继承。< / b>


您可能会说将TableName的值传递给构造函数 ,但我认为

,作者bizobj应该知道它的表,没有将它的名字传递给

它。


你可能会问为什么要在构造函数中调用LoadData?或者说

在实例化AuthorBizObj之后调用LoadData,而不是在

构造函数中 - 我其实不是在创建bizobjs,这只是一些示例

代码来演示这个问题,我试图覆盖一个成员字段,

不是数字应该调用LoadData的位置。


谢谢,

-

Rick Hodder

解决方案

这样的事情怎么样?这里名字在调用WriteLine之前设置。


class BaseObject

{

protected string name = string.Empty;


protected BaseObject()

{

Init();

}


protected virtual void Init()

{

System.Diagnostics.Debug.WriteLine(name);

}

}


类DerivedObject:BaseObject

{

public DerivedObject()

{

}


protected override void Init()

{

name =" MyTable";

base.Init();

}

}

RickHodder写道:< blockquote class =post_quotes>我遇到了问题,这里有一个代码的简化示例,用于演示它:

公共类BizObj
{
public string TableName ="";
private DataSet oData;

public BizObj()
{
LoadData();
公共void void LoadData()
{/> SqlConnection oConn = new SqlConnection(" ...");
SqlDataAdapter oAdapter = new SqlDataAdapter(" SELECT * FROM
" + this.TableName,oConn);
oAdapter.Fill(oData);

}
}

public class AuthorBizObj:BizObj
{
public AuthorBizObj()
this

现在,因为基类构造函数首先触发,所以在将TableName设置为Authors之前调用LoadData


所以我尝试覆盖TableName字段以下方式,希望它将在构造函数方法之前设置为fire:

public class AuthorBizObj:BizObj
{public string TableName =" Authors" ;

公共AuthorBizObj()
{
}
}

但我得到一个编译警告:关键字new是必需的
AuthorBizObj.TableName,因为它隐藏了继承的成员BizObj.TableName。

我不是要隐藏成员,我试图覆盖它。

如何确保TableName字段的值为Authors?
之前的BizObj构造函数是什么?

< b>这个问题严格来说是继承。< / b>

你可能会说传递一个值对于TableName到构造函数 ,但我认为作者bizobj应该知道它的表格,而不是将它的名字传递给
它。

你可能会问:为什么要调用LoadData in构造函数?或者说
在实例化AuthorBizObj之后调用LoadData,而不是在
构造函数中 - 我实际上并不是在创建bizobjs,这只是一些示例代码,用于演示问题,我试图覆盖成员字段,
不知道应该调用LoadData的位置。 br />
谢谢,



我不是想隐藏会员,我是我试图覆盖它。


你不能覆盖字段。


你可能会说将TableName的值传递给构造函数 ,


完全正确。


但我认为作者bizobj应该知道它的表,而不是将其名称传递给
它。




我不知道这是怎么阻止你将它传递给BizObj

构造函数的。这不适合你吗?


公共类BizObj

{

公共字符串TableName;


protected BizObj(string tableName)

{

TableName = tableName;

LoadData();

}


...

}


公共类AuthorBizObj:BizObj

{

public AuthorBizObj():base(" Authors")

{

}

}

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


>我遇到了问题,这是一个代码的简化示例,

演示了它:

公共类BizObj
{
公共字符串TableName ="";
私有DataSet oData;
public BizObj()
{
LoadData();
}
public void LoadData()
{sqlConnection oConn = new SqlConnection(" ...");
SqlDataAdapter oAdapter = new SqlDataAdapter(" SELECT * FROM
" + this.TableName, oConn);
oAdapter.Fill(oData);
}
}
公共类AuthorBizObj:BizObj
{
public AuthorBizObj()
{
this.TableName =" Authors";
}
}现在,因为基类构造函数首先触发,所以在TableName可以被调用之前调用LoadData设置为作者。

所以我尝试按以下方式覆盖TableName字段,
希望在构造函数方法触发之前设置它:
{
public string TableName =" Authors";
public AuthorBizObj()
{
}
}
但我得到一个编译警告:
AuthorBizObj.TableName需要关键字new,因为它隐藏了继承的成员
BizObj.TableName。

我不是试图隐藏成员,我试图覆盖它。

如何在BizObj构造函数触发之前确保TableName字段的值为作者

< b>这个问题严格来说是继承。< / b>

您可能会说将TableName的值传递给构造函数。 ,但我认为作者bizobj应该知道它的表格,而不是将其名称传递给它。

您可能会问:为什么要调用LoadData in构造函数?
或说在您实例化AuthorBizObj之后调用LoadData,而不是在构造函数中 - 我实际上并不是在创建bizobjs,这只是一些演示问题的示例代码,我试图覆盖成员字段,而不是弄清楚应该调用LoadData的位置。 br />
谢谢,




我认为覆盖或隐藏或隐藏一个领域是坏消息。怎么样。


公共抽象类BizObj

{

private readonly string TableName = string.Empty;


protected BizObj(string tableName)

{

this.TableName = tableName;

Console.WriteLine(" ; BizObj Ctor");

LoadData();

}


protected void LoadData()

{

Console.WriteLine(" TableName:{0}",TableName);

}

}


公共类AuthorBizObj:BizObj

{

public AuthorBizObj():base(" Author")

{

Console.WriteLine(" AuthorBizObj Ctor。");

}

}


I''m having a problem, and here is a simplified example of code that
demonstrates it:
public class BizObj
{
public string TableName="";
private DataSet oData;

public BizObj()
{
LoadData();
}

public void LoadData()
{
SqlConnection oConn = new SqlConnection("...");
SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM
"+this.TableName,oConn);
oAdapter.Fill(oData);

}
}

public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName="Authors";
}
}

Now, because the base class constructor fires first, LoadData is called
before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner, hoping
that it will be set before the constructor methods fire:
public class AuthorBizObj: BizObj
{
public string TableName="Authors";

public AuthorBizObj()
{
}
}

But I get a compile warning: "The keyword new is required on
AuthorBizObj.TableName because it hides inherited member BizObj.TableName."

I''m not trying to hide the member, I''m trying to override it.

How can I make sure that the TableName field has the value "Authors" before
the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I think
that the Authors bizobj should know its table, not have its name passed into
it.

You might ask "Why do you want to call LoadData in the constructor?" or say
"Call LoadData after you have instantiated AuthorBizObj, not in the
constructor" - I''m actually not creating bizobjs, this is just some sample
code that demonstrates the problem, I''m trying to override a member field,
not figure out where LoadData should be called.

Thanks,
--
Rick Hodder

解决方案

What about something like this? Here name gets set before the WriteLine is called.

class BaseObject
{
protected string name = string.Empty;

protected BaseObject()
{
Init();
}

protected virtual void Init()
{
System.Diagnostics.Debug.WriteLine(name);
}
}

class DerivedObject : BaseObject
{
public DerivedObject()
{
}

protected override void Init()
{
name = "MyTable";
base.Init();
}
}
RickHodder wrote:

I''m having a problem, and here is a simplified example of code that
demonstrates it:
public class BizObj
{
public string TableName="";
private DataSet oData;

public BizObj()
{
LoadData();
}

public void LoadData()
{
SqlConnection oConn = new SqlConnection("...");
SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM
"+this.TableName,oConn);
oAdapter.Fill(oData);

}
}

public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName="Authors";
}
}

Now, because the base class constructor fires first, LoadData is called
before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner, hoping
that it will be set before the constructor methods fire:
public class AuthorBizObj: BizObj
{
public string TableName="Authors";

public AuthorBizObj()
{
}
}

But I get a compile warning: "The keyword new is required on
AuthorBizObj.TableName because it hides inherited member BizObj.TableName."

I''m not trying to hide the member, I''m trying to override it.

How can I make sure that the TableName field has the value "Authors" before
the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I think
that the Authors bizobj should know its table, not have its name passed into
it.

You might ask "Why do you want to call LoadData in the constructor?" or say
"Call LoadData after you have instantiated AuthorBizObj, not in the
constructor" - I''m actually not creating bizobjs, this is just some sample
code that demonstrates the problem, I''m trying to override a member field,
not figure out where LoadData should be called.

Thanks,



I''m not trying to hide the member, I''m trying to override it.
Well you can''t override fields.

You might say "Pass a value for TableName to the constructor" ,
Exactly.

but I think
that the Authors bizobj should know its table, not have its name passed into
it.



I don''t see how that prevents you from passing it to a BizObj
constructor. Wouldn''t this work for you?

public class BizObj
{
public string TableName;

protected BizObj(string tableName)
{
TableName = tableName;
LoadData();
}

...
}

public class AuthorBizObj : BizObj
{
public AuthorBizObj() : base("Authors")
{
}
}
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


> I''m having a problem, and here is a simplified example of code that

demonstrates it:

public class BizObj
{
public string TableName="";
private DataSet oData;
public BizObj()
{
LoadData();
}
public void LoadData()
{
SqlConnection oConn = new SqlConnection("...");
SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM
"+this.TableName,oConn);
oAdapter.Fill(oData);
}
}
public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName="Authors";
}
}
Now, because the base class constructor fires first, LoadData is
called before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner,
hoping that it will be set before the constructor methods fire:

public class AuthorBizObj: BizObj
{
public string TableName="Authors";
public AuthorBizObj()
{
}
}
But I get a compile warning: "The keyword new is required on
AuthorBizObj.TableName because it hides inherited member
BizObj.TableName."

I''m not trying to hide the member, I''m trying to override it.

How can I make sure that the TableName field has the value "Authors"
before the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I
think that the Authors bizobj should know its table, not have its name
passed into it.

You might ask "Why do you want to call LoadData in the constructor?"
or say "Call LoadData after you have instantiated AuthorBizObj, not in
the constructor" - I''m actually not creating bizobjs, this is just
some sample code that demonstrates the problem, I''m trying to override
a member field, not figure out where LoadData should be called.

Thanks,



I think "overriding" or "hiding" a field is bad news. How about this.

public abstract class BizObj
{
private readonly string TableName = string.Empty;

protected BizObj(string tableName)
{
this.TableName = tableName;
Console.WriteLine("BizObj Ctor");
LoadData();
}

protected void LoadData()
{
Console.WriteLine("TableName: {0}", TableName);
}
}

public class AuthorBizObj : BizObj
{
public AuthorBizObj() : base("Author")
{
Console.WriteLine("AuthorBizObj Ctor.");
}
}


这篇关于构造函数在C#中触发继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆