包装类 [英] Wrapper class

查看:50
本文介绍了包装类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个微不足道的问题,但它让我很难过。


公共类A

{

}


公共舱B:A

{

B(A a)

{

//我希望这个类引用''a'而不复制它。

}

}

想法是你提供基类A的实例并在同一个实例周围包装

类B以提供额外的功能。我想

将班级''A'专门化为班级''B''。我该怎么做?


Dave

This may be a trivial question, but it''s stumping me.

public class A
{
}

public class B : A
{
B(A a)
{
// I want this class to reference ''a'' and not copy it.
}
}

The idea is that you provide an instance of the base class ''A'' and wrap
class ''B'' around the same instance to provide extra functionality. I want to
specialise class ''A'' into class ''B''. How do I do this?

Dave

推荐答案

包装器执行此操作:


来自:Dave Moran - 查看个人资料

日期:2006年5月25日星期四下午6:09

电子邮件:Dave Moran< dmo .. 。@ nospam.nospam>

群组:microsoft.public.dotnet.languages.csharp

尚未评分估价:

显示选项

回复|回复作者|转发|打印|个人信息|显示

原创|举报滥用行为|查找此作者的留言

这可能是一个微不足道的问题,但它让我感到难过。


公共类A

{

public A(stirng ConnectionString){...}

}

公共舱B

{

private A internalA;

public B(stirng ConnectionString)

{

internalA = new A(ConnectionString) ;

}

}


否则你不包装A.你继承它


希望这有帮助

Galin Iliev [MCSD.NET]
www.galcho.com

the wrapper does this:

From: Dave Moran - view profile
Date: Thurs, May 25 2006 6:09 pm
Email: Dave Moran <dmo...@nospam.nospam>
Groups: microsoft.public.dotnet.languages.csharp
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author
This may be a trivial question, but it''s stumping me.

public class A
{
public A(stirng ConnectionString) {...}
}
public class B
{
private A internalA;
public B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}

otherwise you do not wrap A. you subclass it

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com


在这种情况下,B不是A;它就像一个人,


这里的一个解决方案是接口IA(即我和你的原始类

名称),它定义了密钥财产等; B然后实现IA(B:IA),

并将方法转发/专门化到它持有的A的实例。


Marc
In this case B isn''t an A; it just behaves like one,

One solution here would be an interface IA (i.e. "I" and your original class
name) which defines the key properties etc; B then implements IA (B : IA),
and forwards / specialises the methods to the instance of A that it holds.

Marc


感谢您的快速回复。我想我正在尝试子类A而不是

包装它。我想引用原文,而不是创建一个新的A实例。


如果想要简单引用,需要在B的构造函数中发生什么?
A(即子类)?我还需要将一个A的实例传入

构造函数,而不是A的数据。


Dave


公共类A

{

public A(string ConnectionString){...}

}

公共舱B:A

{

公共B(A a)

{

internalA = new A(ConnectionString);

}

}


" Galcho [MCSD.NET]"写道:
Thanks for the speedy reply. I think I''m trying to subclass A rather than
wrap it. I want to reference the original, not create a new instance of A.

What needs to happen in the constructor of B if want it to simply reference
A (i.e. subclass)? I also need to pass in an instance of A into the
constructor, not the data for A.

Dave

public class A
{
public A(string ConnectionString) {...}
}

public class B : A
{
public B(A a)
{
internalA = new A(ConnectionString);
}
}

"Galcho[MCSD.NET]" wrote:
包装器执行此操作:

来自:Dave Moran - 查看个人资料
日期:2006年5月25日星期四下午6:09 <电子邮件:Dave Moran< dmo ... @ nospam.nospam>
群组:microsoft.public.dotnet.languages.csharp
尚未评分评分:
显示选项
回复|回复作者|转发|打印|个人信息|显示
原创|举报滥用行为|查找该作者的信息

这可能是一个微不足道的问题,但它让我感到困惑。

公共课A
{/ / (stirng ConnectionString){...}
}
公共班级B
{私人A internalA;
公共B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}
否则你不包装A.你将它子类化
希望这有帮助
Galin Iliev [MCSD.NET]
www.galcho.com
the wrapper does this:

From: Dave Moran - view profile
Date: Thurs, May 25 2006 6:09 pm
Email: Dave Moran <dmo...@nospam.nospam>
Groups: microsoft.public.dotnet.languages.csharp
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author
This may be a trivial question, but it''s stumping me.

public class A
{
public A(stirng ConnectionString) {...}
}
public class B
{
private A internalA;
public B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}

otherwise you do not wrap A. you subclass it

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com



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

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