帮助继承和属性 [英] Help with inheritence and properties

查看:90
本文介绍了帮助继承和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本表单和一个基本逻辑类。每个人都必须知道另一个。

然后我继承创建后代形式和后代逻辑

扩展两个对象并再次必须知道彼此。我在语法/技术方面已经混淆了

,关于如何在代码中一般性地引用引用。我希望能够始终调用LogicClass.SomeMethod()和每个

FormClass知道如何处理它。在某些情况下,我有3-4级深度。


我无法理解究竟发生了什么或者它是否正确,即使

某些方式似乎工作。我正在寻找一种一致的方式,因为我看起来像这样代码就像这样。有时候我似乎和

混在一起试图投降......我很困惑。


1)这是一个浪费资源或基类变量甚至没有创建?b $ b?我假设如果我总是在baseForm和childForm中的所有方法中通过Logic

属性访问它,那么
总是会引用相应的_logic(在childForm的情况

永远不会创建BaseLogic _logic ???)

Class BaseForm {

protected BaseLogic _logic = new BaseLogic();

public BaseLogic Logic {get {return _logic;}}

class ChildForm:BaseForm {

protected new ChildLogic _logic = new ChildLogic();

public new ChildLogic Logic {get {return _logic;}}

}


是在每个继承的对象中实例化_logic是不好的做法?如果我

我不会陷入ChildForm,我无法做到(ChildLogic)_logic类型

操作...


2)上面的示例当我调用以下方法时,在ChildForm中没有
覆盖真正发生的事情是什么?


Class BaseForm {

...

public void TestMethod(){

Logic.SomeMethod()

}


SomeOtherClass {

childForm = new ChildForm();

childForm.TestMethod()

这是_logic使用的
?在我看来,它使用了

ChildForm._logic,只要ChildForm._logic有一个SomeMethod


我也尝试过其他方式我有一个方法来实现逻辑

对象,然后我覆盖后代类中的instatiate方法。

确保我只创建最深层次的逻辑对象。


有点啰嗦但我真的可以帮忙..


thx


杰克

I have a base form and a base logic class. Each has to know of the other.
I''m then inheriting to create descendant form and descendant logic which
extend both objects and again have to know of each other. I''m getting mixed
up in the syntax/technique on how to refer to the references generically in
the code. I want to be able to always call LogicClass.SomeMethod() and each
FormClass knows what to do with it. In some cases I have 3-4 levels deep.

I can''t rationalize what exactly is happening or if its correct, even if
certain ways seem to work. I''m looking for a consistent manner since I seem
to code like this all over the place. Sometimes I seem to get mixed up with
trying to cast up or down... I''m getting quite confused.

1) Is this a waste of resources or does the base class variables not even
get created? I''m assuming then that if I always access throught the Logic
property in all the methods throughout both baseForm and childForm it will
always refer to the coresponding _logic (in the case of childForm the
BaseLogic _logic is never created???)

Class BaseForm{
protected BaseLogic _logic = new BaseLogic( );
public BaseLogic Logic { get{ return _logic;}}

class ChildForm : BaseForm{
protected new ChildLogic _logic = new ChildLogic( );
public new ChildLogic Logic { get{ return _logic;}}
}

Is it bad practice to instantiate the _logic in each inherited object? If I
don''t I get stuck in ChildForm where I can''t do a (ChildLogic)_logic type
operation...

2) With the example above when I call the following method that isn''t
overriden in ChildForm what really happens?

Class BaseForm{
...
public void TestMethod(){
Logic.SomeMethod()
}

SomeOtherClass {
childForm = new ChildForm();
childForm.TestMethod()
}

which _logic does this use? It seems to me that it uses the
ChildForm._logic and as long as the ChildForm._logic has a SomeMethod

I''ve also tried other ways where I have a method to instatiate the logic
object and then I override the instatiate method in the descendent class.
That assures that I only create the deepest level descended logic objects.

Kinda long winded but I really could use some help..

thx

jack

推荐答案

尝试这样的事情:


class BaseForm

{

受保护的BaseLogic逻辑;

public BaseForm()

{

logic = CreateLogic() ;

}

protected virtual BaseLogic CreateLogic()

{

返回新的BaseLogic();

}

}


class ChildForm:BaseForm

{

protected override BaseLogic CreateLogic()

{

返回新的ChildLogic();

}

}


现在你的代码应该可以工作。


之前的问题是,从基类中你的逻辑成员引用逻辑的基本版本,因此调用它的函数版本。如果它引用了子版本(这是我上面的代码将实现的),你将获得调用的派生版本。


问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/ weblog


我有一个基本表单和一个基本逻辑类。每个人都必须知道另一个。

然后我继承创建后代形式和后代逻辑

扩展两个对象并再次必须知道彼此。我在语法/技术方面已经混淆了

,关于如何在代码中一般性地引用引用。我希望能够始终调用LogicClass.SomeMethod()和每个

FormClass知道如何处理它。在某些情况下,我有3-4级深度。
Try something like this:

class BaseForm
{
protected BaseLogic logic;
public BaseForm()
{
logic = CreateLogic();
}
protected virtual BaseLogic CreateLogic()
{
return new BaseLogic();
}
}

class ChildForm : BaseForm
{
protected override BaseLogic CreateLogic()
{
return new ChildLogic();
}
}

now your code should work.

The problem before is that from the base class your logic member refers to the base version of the logic and so its version of the function gets called. If it referred to the child version (which is what my code above will achieve) you will get the derived version called.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I have a base form and a base logic class. Each has to know of the other.
I''m then inheriting to create descendant form and descendant logic which
extend both objects and again have to know of each other. I''m getting mixed
up in the syntax/technique on how to refer to the references generically in
the code. I want to be able to always call LogicClass.SomeMethod() and each
FormClass knows what to do with it. In some cases I have 3-4 levels deep.


理查德,


非常感谢。我假设这完全独立于添加一个

属性来执行GetLogic?虽然现在我不需要在图表上一直复制

属性。


现在我需要将其转换为(ChildLogic)每次我想在@B $ b的ChildForm中使用它的逻辑?或者我可以使用房产进行投票吗?


" Richard Blewett [DevelopMentor]" < RI ****** @ develop.com>在消息中写道

news:u4 ************** @ tk2msftngp13.phx.gbl ...
Richard,

Thanks so much. I assuming that this is all independent of adding a
property to do a GetLogic? All though now I don''t need to duplicate the
property all the way up the chart.

Now do I need to cast it to (ChildLogic)logic everytime I want to use it in
the ChildForm? or can I use a property to do the cast???

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:u4**************@tk2msftngp13.phx.gbl...
尝试这样的事情:

类BaseForm
{
受保护的BaseLogic逻辑;
公共BaseForm()
{
logic = CreateLogic();
}
受保护的虚拟BaseLogic CreateLogic()
返回新的BaseLogic();
}

类ChildForm:BaseForm
{
受保护的覆盖BaseLogic CreateLogic()
{
返回新的ChildLogic();
}
}
现在你的代码应该以前的问题是,从基类中你的逻辑成员引用逻辑的基本版本,因此它的函数版本被调用。如果它引用了子版本(这是我上面的代码将会实现的),你将获得所谓的派生版本。

问候

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

我有一个基本表单和一个基本逻辑类。每个人都必须知道
其他。
然后我继承创建后代形式和后代逻辑,这些逻辑延伸两个对象并且必须彼此了解。我在语法/技术方面已经得到了如何在代码中一般性地引用引用的方法。我希望能够始终调用LogicClass.SomeMethod()和
每个FormClass都知道如何处理它。在某些情况下,我有3-4级深度。
Try something like this:

class BaseForm
{
protected BaseLogic logic;
public BaseForm()
{
logic = CreateLogic();
}
protected virtual BaseLogic CreateLogic()
{
return new BaseLogic();
}
}

class ChildForm : BaseForm
{
protected override BaseLogic CreateLogic()
{
return new ChildLogic();
}
}

now your code should work.

The problem before is that from the base class your logic member refers to
the base version of the logic and so its version of the function gets
called. If it referred to the child version (which is what my code above
will achieve) you will get the derived version called.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I have a base form and a base logic class. Each has to know of the
other.
I''m then inheriting to create descendant form and descendant logic which
extend both objects and again have to know of each other. I''m getting
mixed
up in the syntax/technique on how to refer to the references generically
in
the code. I want to be able to always call LogicClass.SomeMethod() and
each
FormClass knows what to do with it. In some cases I have 3-4 levels deep.



这是一个不同的实现,不需要从基类调用虚拟成员构造函数(通常可以避免的,因为派生类构造函数体在调用方法时尚未运行)


类BaseForm

{

保护BaseLogic逻辑;

public BaseForm()

{

logic = new BaseLogic(); < br $>
}

保护BaseForm(BaseLogic逻辑)

{

this.logic = logic;

}

}


类ChildForm:BaseForm

{

public ChildForm()

:base(新的ChildLogic)//或者这个(新的ChildLogic())

{

}

受保护的ChildForm(BaseLogic逻辑)

:基数(逻辑)

{

}

}


是否需要每个级别都有自己的属性转向逻辑取决于派生的逻辑类是否简单地覆盖在BaseLogic中声明的功能,或者它们是否添加新方法,prooperties等。如果是前者那么你可以在BaseForm中有一个属性返回BaseLogic并且不需要强制转换作为被覆盖的将调用函数的版本来代替基本函数。如果添加了新方法,那么派生版本最好拥有自己的属性来隐藏基本版本,返回对逻辑的派生引用


问候


Richard Blewett - DevelopMentor
http:// staff.develop.com/richardb/weblog

理查德,


非常感谢。我假设这完全独立于添加一个

属性来执行GetLogic?虽然现在我不需要在图表上一直复制

属性。


现在我需要将其转换为(ChildLogic)每次我想在@B $ b的ChildForm中使用它的逻辑?或者我可以使用属性进行演员表演
Heres a different implementation that doesn''t require calling virtual members from base class constructors (something to generally be avoided because the derived class constructor body hasn''t yet run when it''s method gets called)

class BaseForm
{
protected BaseLogic logic;
public BaseForm()
{
logic = new BaseLogic();
}
protected BaseForm( BaseLogic logic)
{
this.logic = logic;
}
}

class ChildForm : BaseForm
{
public ChildForm()
: base( new ChildLogic ) // or this(new ChildLogic() )
{
}
protected ChildForm( BaseLogic logic )
: base(logic)
{
}
}

Whether you need each level to have its own property to return the Logic depends on whether the derived Logic classes simply override functionality declared in BaseLogic or whether they add new methods, prooperties, etc. If the former then you can have one property in BaseForm that returns the BaseLogic and no casting is necessary as the overridden versions of functions will get called in place of the base ones. If new methods are being added then it is probably best for the derived versions to have their own property which hides the base version, returning a derived reference to the Logic

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
Richard,

Thanks so much. I assuming that this is all independent of adding a
property to do a GetLogic? All though now I don''t need to duplicate the
property all the way up the chart.

Now do I need to cast it to (ChildLogic)logic everytime I want to use it in
the ChildForm? or can I use a property to do the cast???


这篇关于帮助继承和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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