对泛型和抽象类感到困惑 [英] confused about generics and abstract classes

查看:89
本文介绍了对泛型和抽象类感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我可能看不出明显的,但也许你可以帮我解决这个问题。

一塌糊涂。


以下是我的问题:

我创建了两个类NormDistribution和DiscDistribution。两个

类都提供了运算符+的实现。


现在我想编写另一个通用类Plan< DType>,它可以

接受DType的NormDistribution和DiscDistribution。这里来了

我的问题。课程计划必须具有以下功能:


公共课程计划< DType>

{

公共DType Demand1;

public DType Demand2;

public DType Demand3;


public void Optimize()

{

Demand3 = Demand1 + Demand2; //< - 错误CS0019

}

}


如何解决问题?

这个背景是,我的班级计划进行了一些计算,

与底层分布类型无关。

计算的区别仅在于基本运算符的实现+,

- 等等。我想把问题委托给NormDistribtution

和DiscDistribution类,但不知何故我无法让它工作。


有什么想法吗?


谢谢,

Sascha

解决方案

2006年5月9日02: 05:07 -0700, he*****@wiso.uni-koeln.de 写道:

以下是我的问题:
我创建了两个类NormDistribution和DiscDistribution。两个类都提供了操作符+的实现。




从您的代码判断,看起来好像您期望.NET / C#generics

像C ++泛型一样工作 - 但他们不是。它们使用运行时实例化,而不是像C ++中那样编译时实例化,因此它们可以在不同的程序集中使用,而不是在它们定义的程序集中使用它们。 。


因此,你的_concrete_类型是否定义某种方法并不重要。你的_type argument_必须通过约束声明你想要在泛型类或方法中使用的任何方法




即便如此,你赢了''至少可以使用运算符,因为.NET

泛型目前不接受运算符限制。你必须定义

a常规方法来添加。


没有冒犯,但我建议你拿起MSDN Library或一本好书

(请参阅我的网站)并阅读.NET泛型如何工作。对于习惯于C ++泛型的人来说,有很多问题。杰夫里希特的

" CLR via C#"涵盖了您在此方面需要了解的所有内容。

-
http://www.kynosarges.de




< he ***** @ wiso.uni-koeln &由Matchi.com提供回到GT;在消息中写道

news:11 ********************* @ y43g2000cwc.googlegro ups.com ...

你好,

我可能没有看到明显的,但也许你可以帮助我摆脱这个混乱。

以下是我的问题:
我创建了两个类NormDistribution和DiscDistribution。这两个类都提供了操作符+的实现。

现在我想编写另一个泛型类Plan< DType>,它可以接受NypeDistribution和DiscDistribution for DType。这是我的问题。课程计划必须具有以下功能:

公共课程计划< DType>
公共DType Demand1;
公共DType Demand2;
公共DType Demand3;

public void Optimize()
{
Demand3 = Demand1 + Demand2; //< - 错误CS0019
}
}
如何解决问题?
背景是,我的班级计划进行了一些计算,
与下属分布类型无关。
计算仅在基本运算符+,
等的实现方面有所不同。我想将问题委托给NormDistribtution
和DiscDistribution类,但不知怎的,我无法让它工作。

任何想法?

谢谢,
Sascha




1)您的分发类是从公共基础派生的还是

实现了一个通用接口(让我们来电话)它是共同的两种方式)。

2)给予共同的公共共同添加(共同的其他)。方法

3)强制DType派生自(或实现)Common:public class

Plan< DType>其中DType:Common {...."

4)Demand3 = Demand1.Add(Demand2);


这不是真的令人满意,因为你不妨忘记

泛型,只需使用Common即可。使用普通的旧多态性。


正如克里斯所指出的那样 - 即使忽略运算符或方法的问题,你也不能做通常的C ++内容。


例如,您无法将方法注入到派生类中:


公共类DiscDistribution:IAddable< DiscDistribution>

1000道歉 - 我在考虑C类< T> :T"或C< A,B> :A< B>" ;.


您的其他帖子确实是要走的路和您将要做的事情

类似于C ++的事情


Jay B. Harlow [MVP - Outlook]" < JA ************ @ tsbradley.net>写在

消息新闻:ur ************** @ TK2MSFTNGP02.phx.gbl ...

Nick, |例如,您无法使用以下方法将方法注入派生类:
|公共类DiscDistribution:IAddable< DiscDistribution>
嗯...是的,你可以!

尝试:

公共接口IAddable< T>
{
T添加(T其他);
}
公共类DiscDistribution:IAddable< DiscDistribution>
{
公共DiscDistribution Add(DiscDistribution other)
{
返回null;
}

我使用上面的IEquatable< T>所有的时间!

-
希望这会有所帮助
Jay B. Harlow [MVP - Outlook]
.NET Application Architect,Enthusiast,&福音传道者
T.S.布拉德利 - http://www.tsbradley.net

" ; Nick Hounsome <氖** @ NickHounsome.Me.Uk>在消息中写道
新闻:1V ******************** @ fe2.news.blueyonder.co .uk ...
|
| <他***** @ wiso.uni-koeln.de>在消息中写道
|新闻:11 ********************* @ y43g2000cwc.googlegro ups.com ...
| >你好,
| >
| >我可能没有看到明显的,但也许你可以帮助我
这个
| >一团糟。
| >
| >以下是我的问题:
| >我创建了两个类NormDistribution和DiscDistribution。两者都是
| >类提供了运算符+的实现。
| >
| >现在我想写另一个通用类Plan< DType>,它可以
| >接受DType的NormDistribution和DiscDistribution。这里来了
| >我的问题。课程计划必须具有以下功能:
| >
| >公共课计划< DType>
| > {
| > public DType Demand1;
| > public DType Demand2;
| > public DType Demand3;
| >
| > public void Optimize()
| > {
| > Demand3 = Demand1 + Demand2; //< - 错误CS0019
| > }
| > }
| >
| >我该如何解决这个问题?
| >背景是,我的班级计划做了一些计算,
| >它们与下属分配类型无关。
| >计算只在基本运算符的实现上有所不同
+,
| > - 等等。我想将问题委托给NormDistribtution
| >和DiscDistribution类,但不知怎的,我无法让它工作。
| >
| >有什么想法吗?
| >
| >谢谢,
| > Sascha
| >
|
| 1)您的分发类来自公共基础或
|实现一个通用接口(让我们称之为Common,无论哪种方式)。
| 2)给予Common一个public Common Add(Common other)。方法
| 3)强制DType派生自(或实现)Common:public class
|计划和LT; D型>其中DType:Common {...."
| 4)Demand3 = Demand1.Add(Demand2);
|
|这真的不令人满意,因为你不妨忘记
|泛型,只是与共同一起工作使用普通的旧多态性。
|
|克里斯指出 - 甚至无视操作员或方法的问题

|不能做通常的C ++东西。
|
|例如,你不能用以下方法将方法注入派生类:
|
|公共类DiscDistribution:IAddable< DiscDistribution>
|
|
|



Hello,

I probably don''t see the obvious, but maybe you can help me out of this
mess.

The following is my problem:
I created two classes NormDistribution and DiscDistribution. Both
classes provide an implemation of the operator +.

Now I want to write another generic class Plan<DType>, which can
accept NormDistribution and DiscDistribution for DType. And here comes
my problem. Class Plan must have a function like this:

public class Plan<DType>
{
public DType Demand1;
public DType Demand2;
public DType Demand3;

public void Optimize()
{
Demand3 = Demand1 + Demand2; // <- error CS0019
}
}

How can I solve the problem?
The background for this is, that my class Plan does some computations,
which are independant of the underling distribution type. The
computation differ only in the implementation of the basic operators +,
- and so on. I wanted to delegate the problem to the NormDistribtution
and DiscDistribution class, but somehow I can not get it to work.

Any ideas?

Thanks,
Sascha

解决方案

On 9 May 2006 02:05:07 -0700, he*****@wiso.uni-koeln.de wrote:

The following is my problem:
I created two classes NormDistribution and DiscDistribution. Both
classes provide an implemation of the operator +.



Judging from your code, it looks as if you expected .NET/C# generics
to work like C++ generics -- but they don''t. They use run-time
instantiation, not compile-time instantiation as in C++, so that they
can be used from different assemblies than the one they''re defined in.

For this reason, it doesn''t matter whether your _concrete_ types
define some method. Your _type argument_ must declare any methods
that you want to use in the generic class or method, via constraints.

And even so, you won''t be able to use operators at all because .NET
generics do not currently accept operator restraints. You must define
a regular method for addition instead.

No offense, but I suggest you pick up the MSDN Library or a good book
(see my website) and read up on how .NET generics work. There''s a lot
of gotchas for people who are used to C++ generics. Jeff Richter''s
"CLR via C#" covers everything you need to know in this regard.
--
http://www.kynosarges.de



<he*****@wiso.uni-koeln.de> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...

Hello,

I probably don''t see the obvious, but maybe you can help me out of this
mess.

The following is my problem:
I created two classes NormDistribution and DiscDistribution. Both
classes provide an implemation of the operator +.

Now I want to write another generic class Plan<DType>, which can
accept NormDistribution and DiscDistribution for DType. And here comes
my problem. Class Plan must have a function like this:

public class Plan<DType>
{
public DType Demand1;
public DType Demand2;
public DType Demand3;

public void Optimize()
{
Demand3 = Demand1 + Demand2; // <- error CS0019
}
}

How can I solve the problem?
The background for this is, that my class Plan does some computations,
which are independant of the underling distribution type. The
computation differ only in the implementation of the basic operators +,
- and so on. I wanted to delegate the problem to the NormDistribtution
and DiscDistribution class, but somehow I can not get it to work.

Any ideas?

Thanks,
Sascha



1) Either have your distribution classes derive from a common base or
implement a common interface (Let''s call it "Common" either way).
2) Give Common an "public Common Add(Common other)" method
3) Force DType to be derived from (or implement) Common: "public class
Plan<DType> where DType: Common {...."
4) Demand3 = Demand1.Add(Demand2);

This isn''t really satisfactory because you might as well forget about
generics and just work with "Common" using plain old polymorphism.

As Chris pointed out - even ignoring the issue of operators or methods you
cannot do the usual C++ stuff.

eg you cannot inject methods into a derived class with:

public class DiscDistribution : IAddable<DiscDistribution>


1000 apologies - I was thinking of "class C<T> : T" or "C<A,B> : A<B>".

Your other post is indeed the way to go and the way that you would do
something similar in C++

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:ur**************@TK2MSFTNGP02.phx.gbl...

Nick,
| eg you cannot inject methods into a derived class with:
| public class DiscDistribution : IAddable<DiscDistribution>
Umm... Yes you can!

Try:

public interface IAddable<T>
{
T Add(T other);
}

public class DiscDistribution : IAddable<DiscDistribution>
{
public DiscDistribution Add(DiscDistribution other)
{
return null;
}
}

I use the above with IEquatable<T> all the time!
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote in message
news:1V********************@fe2.news.blueyonder.co .uk...
|
| <he*****@wiso.uni-koeln.de> wrote in message
| news:11*********************@y43g2000cwc.googlegro ups.com...
| > Hello,
| >
| > I probably don''t see the obvious, but maybe you can help me out of
this
| > mess.
| >
| > The following is my problem:
| > I created two classes NormDistribution and DiscDistribution. Both
| > classes provide an implemation of the operator +.
| >
| > Now I want to write another generic class Plan<DType>, which can
| > accept NormDistribution and DiscDistribution for DType. And here comes
| > my problem. Class Plan must have a function like this:
| >
| > public class Plan<DType>
| > {
| > public DType Demand1;
| > public DType Demand2;
| > public DType Demand3;
| >
| > public void Optimize()
| > {
| > Demand3 = Demand1 + Demand2; // <- error CS0019
| > }
| > }
| >
| > How can I solve the problem?
| > The background for this is, that my class Plan does some computations,
| > which are independant of the underling distribution type. The
| > computation differ only in the implementation of the basic operators
+,
| > - and so on. I wanted to delegate the problem to the NormDistribtution
| > and DiscDistribution class, but somehow I can not get it to work.
| >
| > Any ideas?
| >
| > Thanks,
| > Sascha
| >
|
| 1) Either have your distribution classes derive from a common base or
| implement a common interface (Let''s call it "Common" either way).
| 2) Give Common an "public Common Add(Common other)" method
| 3) Force DType to be derived from (or implement) Common: "public class
| Plan<DType> where DType: Common {...."
| 4) Demand3 = Demand1.Add(Demand2);
|
| This isn''t really satisfactory because you might as well forget about
| generics and just work with "Common" using plain old polymorphism.
|
| As Chris pointed out - even ignoring the issue of operators or methods
you
| cannot do the usual C++ stuff.
|
| eg you cannot inject methods into a derived class with:
|
| public class DiscDistribution : IAddable<DiscDistribution>
|
|
|



这篇关于对泛型和抽象类感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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