且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在泛型类的类型类中调用静态方法?

更新时间:2022-12-16 18:38:17

steve bull< bu **** @ comcast.net>写道:
steve bull <bu****@comcast.net> wrote:
我有一个SwatchPanel类,它将Swatch作为参数类型。
如何在Swatch类中调用静态函数?
I have a class SwatchPanel which takes Swatch as a parameter type.
How can I call a static function within the Swatch class?


>
为什么不直接调用Swatch.Exists,即不要在

中使用泛型参数?你不可能使用被覆盖的版本的Swatch.Foo,

但是你无论如何都不能覆盖静态方法。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

如果回复小组,请不要给我发邮件



Why not just call Swatch.Exists, i.e. don''t use the generic argument at
all? You won''t be able to use an "overridden" version of Swatch.Foo,
but then you can''t override static methods anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




在我看来,仿制药的实现只允许进行最微不足道的练习 - 没有任何基于类型做任何不同的工作,只允许复制和变化很少。

例如,如果我想写

公共类SwatchPanel< TSwatch> :System.Windows.Forms.Panel其中TSwatch:Swatch

{

List< TSwatch> swatchList;


public MethodA

{

Swatch.Write(swatchList);

}

}

有不同的方法根据类型写出样本。编译器似乎没有让我。

我不能说TSwatch.Write,当我在Swatch类下编写以下内容

公共类Swatch

{

public static void Write(List< Swatch> colorSwatchList)

{

写入swatch文件

}


public static void Write(List< ColorSwatch> colorSwatchList)

{

写入颜色样本文件>
}


public static void Write(List< ShadeSwatch> shadeSwatchList)

{

写入阴影样本文件

}

}

编译器告诉我


无法转换' 'System.Collections.Generic.List< TSwatch>''到''System.Collections.Generic.List< NewControls.Swatc h>''


总是拿起第一个覆盖。如果我用ColorSwatch覆盖切换Swatch覆盖,它将要转换为

ColorSwatch通用列表。



cookie切割外,似乎很难对泛型做任何使用。它们是一个脑死亡的实现。


如果我使SwatchPanel成为普通类并从中派生类,我可以完成上述所有问题。那么问题

就是让Swatch.Write与List< Swatch>一起工作。 swatchList。然后每个子类必须从List< Swatch>转换。到

列表< ColorSwatch>当它想要用适合它的方法进行实际写时。


如果不是这个的话,我很抱歉。但实施起来似乎很糟糕。

谢谢,


史蒂夫



关于周四,2005年12月8日01:56:38 -0000,Jon Skeet [C#MVP]< sk *** @ pobox.com>写道:

it seems to me that the implementation of generics allows nothing but the most trivial of exercises to be done - without a means of
doing anything different based on the type allows only duplication and little variation.
For example if I want to write
public class SwatchPanel<TSwatch> : System.Windows.Forms.Panel where TSwatch : Swatch
{
List<TSwatch> swatchList;

public MethodA
{
Swatch.Write(swatchList);
}
}
to have different methods for writing out the swatches based on type. the compiler does not appear to let me.
I cannot say TSwatch.Write and when I write the following under the Swatch class
public class Swatch
{
public static void Write(List<Swatch> colorSwatchList)
{
write to swatch file
}

public static void Write(List<ColorSwatch> colorSwatchList)
{
write to color swatch file
}

public static void Write(List<ShadeSwatch> shadeSwatchList)
{
write to shade swatch file
}
}
the compiler tells me

Can''t convert from ''System.Collections.Generic.List<TSwatch>'' to ''System.Collections.Generic.List<NewControls.Swatc h>''

it always picks up the 1st override. If I switch the Swatch override with the ColorSwatch override it will want to convert to a
ColorSwatch generic list.


Maybe I am doing something very stupid here but it appears to be very difficult to do anything of any use with generics except
cookie cutting. They are a brain dead implementation.

If I make SwatchPanel a normal class and derive classes from it I can do everything all of the above w/o problem. Then the problem
is with making the Swatch.Write work with List<Swatch> swatchList. Each child class then has to convert from List<Swatch> to
List<ColorSwatch> etc when it wants to do the actual write in the method appropriate to it.

I am sorry if this is not the place to bring this up. But the implementation seems pretty poorly thought out.
Thanks,

Steve


On Thu, 8 Dec 2005 01:56:38 -0000, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
史蒂夫·布尔< bu **** @ comcast.net>写道:
steve bull <bu****@comcast.net> wrote:
我有一个SwatchPanel类,它将Swatch作为参数类型。
如何在Swatch类中调用静态函数?
I have a class SwatchPanel which takes Swatch as a parameter type.
How can I call a static function within the Swatch class?


>为什么不直接调用Swatch.Exists,即不要使用
所有的泛型参数?你不可能使用被覆盖的版本的Swatch.Foo,
但是你无论如何都不能覆盖静态方法。



Why not just call Swatch.Exists, i.e. don''t use the generic argument at
all? You won''t be able to use an "overridden" version of Swatch.Foo,
but then you can''t override static methods anyway.



steve bull< bu **** @ comcast.net&GT;写道:
steve bull <bu****@comcast.net> wrote:
在我看来,仿制药的实现只允许进行最微不足道的练习 - 没有办法根据类型做任何不同的事情只允许复制和变化很小。


这根本不是真的。它不允许那种模板化的C ++

,但坦率地说,我很高兴。


例如,如果我想写的话

公共类SwatchPanel< TSwatch> :System.Windows.Forms.Panel其中
TSwatch:Swatch
{
List< TSwatch> swatchList;

公共方法A
{Swatch.Write(swatchList);
}
}

有不同的方法根据类型写出样本。
编译器似乎没有让我。我不能说TSwatch.Write
当我在Swatch类下写下以下内容


< snip>


的确如此。 IMO,那将是一种糟糕的写作方式。看起来好像

你想在那个阶段使用多态 - 要么有一个类

封装了一个Swatches列表(一般来说)并覆盖了

行为恰当,或者有一个Write方法在Swatch类上使用

多态来计算如何编写每个条目。


或者,有一个实例方法在SwatchPanel中写出它的

列表。

也许我在这里做了一些非常愚蠢的事情,但似乎很难做任何与之有关的事情。除了饼干切割之外的仿制药。他们是一个脑死亡的实施。


Cookie切割是一个很好的例子,说明了泛型的用途。

方式你试图做的事情并不是他们打算如何使用,

但这并不意味着仿制药是坏的。你想要做什么打破

以各种方式对象的方向 - 例如,通过在基类中使用一组

静态方法,你是包括将每个

的子类知道到Swatch中的子类到那个基类,当那个

知识应该在子类本身,或者最坏的时候

SwatchPanel的专用子类。

如果我使SwatchPanel成为普通类并从中派生类,我可以完成以上所有W / o问题。然后问题是
使Swatch.Write与List< Swatch>一起工作swatchList。每个孩子
类必须转换为List< Swatch>列出< ColorSwatch>等等什么时候想要用适合它的方法进行实际写作。

如果不是这个的话,我很抱歉。但是
的实现似乎很难想到。
it seems to me that the implementation of generics allows nothing but
the most trivial of exercises to be done - without a means of doing
anything different based on the type allows only duplication and
little variation.
That''s not true at all. It doesn''t allow the kind of templating C++
does, but frankly I''m glad of that.

For example if I want to write

public class SwatchPanel<TSwatch> : System.Windows.Forms.Panel where
TSwatch : Swatch
{
List<TSwatch> swatchList;

public MethodA
{
Swatch.Write(swatchList);
}
}
to have different methods for writing out the swatches based on type.
the compiler does not appear to let me. I cannot say TSwatch.Write
and when I write the following under the Swatch class
<snip>

Indeed. And that would be a bad way of writing it, IMO. It looks like
you want polymorphism at that stage - either have a class which
encapsulates a list of Swatches (generically) and override the
behaviour appropriately, or have one Write method which uses
polymorphism on the Swatch class to work out how to write each entry.

Alternatively, have an instance method in SwatchPanel which writes its
list out.
Maybe I am doing something very stupid here but it appears to be very
difficult to do anything of any use with generics except cookie
cutting. They are a brain dead implementation.
Cookie-cutting is a good example of exactly what generics are for. The
way you''re trying to do things isn''t how they''re intended to be used,
but that doesn''t mean generics are bad. What you''re trying to do breaks
object orientation in various ways - for instance, by having a set of
static methods in the base class, you''re including knowledge of each of
the subclasses into Swatch into the that base class, when that
knowledge should either be in the subclasses themselves, or at worst
the specialised subclasses of SwatchPanel.
If I make SwatchPanel a normal class and derive classes from it I can
do everything all of the above w/o problem. Then the problem is with
making the Swatch.Write work with List<Swatch> swatchList. Each child
class then has to convert from List<Swatch> to List<ColorSwatch> etc
when it wants to do the actual write in the method appropriate to it.

I am sorry if this is not the place to bring this up. But the
implementation seems pretty poorly thought out.




有一些限制确实存在问题,但我没有
我想我同意你到目前为止所提出的任何事情实际上是一个带有仿制药的问题。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http ://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件



There are limitations which are genuinely problematic, but I don''t
think I''d agree that anything you''ve brought up so far is actually a
problem with generics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too