CS0536的基本原理? [英] Rationale for CS0536?

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

问题描述

大家好,


我还是C#的新手,所以也许有人可以解释一下我的理由


CS0536:' 'someClass''没有实现接口成员

''some.Interface.Member()''。 ''someClass.Member()''要么是静态的,要么是公共的b $ b $,或者返回类型错误。


为什么我不能拥有用静态

方法实现接口的类? IIRC,在Java或C ++中,通过类名(someClass :: Member())或通过

对象实例调用静态成员

是可能的。 。


我的动机是我必须使用数学/随机的

分布。因此我基本上有一个


接口分配{

双倍概率(双x);

}


和几个实现它的类。此外,有一些

计算方法返回分布,因此它们给我

分布实例。好吧到目前为止:)


现在有标准的正态分布。你可能还记得你的数学课上的
,它并不依赖于任何特定的参数

并且是常用的。所以我想在很多地方打电话给

StdNormalDistribution :: probability()等。

另外,StdNormalDistribution is_a发布可能是

由上述计算方法返回。所以如果StdNormalDistribution可以作为一个实例返回,那将是很好的

很好

在需要时被静态调用。


有一个好的在C#中解决这个问题的方法?是我的oop设计

坏了吗?我是否忽略了按原样这样做的理由?


[当然是其他方式 - 让界面静态但

实例非静态无法工作;我甚至不确定静态是否静态在接口中使用

感觉?]


非常感谢,

Matthias

Hi all,

I''m still new to C# so maybe someone can explain me the rationale for

CS0536: ''someClass'' does not implement interface member
''some.Interface.Member()''. ''someClass.Member()'' is either static, not
public, or has the wrong return type.

Why can''t I have a class implementing the interface with static
methods? IIRC, in Java or C++ it''s possbile to call a static member
either through the class name (someClass::Member()) or through an
object instance.

My motivation is that I have to work with mathematical/stochastical
distributions. Thus I basically have an

interface Distribution {
double probability(double x);
}

and several classes implementing it. Additionally, there are some
calculation methods that return "Distribution"s, so they give me
instances of Distributions. Allright so far :)

Now there''s the standard normal distribution. As you might remember
from your math classes, it does not depend on any specific parameters
and is commonly used. So I would like to call
StdNormalDistribution::probability() and the like in many places.
Additionally, StdNormalDistribution is_a Distribution and may be
returned by the calculation methods mentioned above. So it would be
nice if StdNormalDistribution could be returned as an instance as well
as being called statically when needed.

Is there a "nice" way to work around this in C#? Is my oop design
broken? Am I overlooking a good reason for doing it as-is?

[Of course the "other way round" - having the interface static but the
instance non-static cannot work; I''m not even sure if "static" makes
sense in an interface?]

Thanks a lot,
Matthias

推荐答案

Matthias,


您可以使用静态方法来实现接口,但是你会有

以迂回的方式进行,从实例上的方法

调用静态方法。


当然,这不是你想要的。接口与

对象(实例)紧密相关,而不是类型(类)。虽然你可以使用静态属性,字段和方法实现接口

,但它只是一种在实例中封装功能的迂回方式

(您将访问静态方法,访问本地变量和静态

字段等)。


您应该做的是使你的StdDeviation类成为单例类,

并将实例公开为Distribution接口的实现

(应该命名为IDistribution,如公共命名所推荐的那样) />
convention)通过Instance属性(或GetInstance方法)。这个

的方式,你有一个实现界面的实例。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse .com

< el ************ @ web.de>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Matthias,

You could use static methods to implement interfaces, but you would have
to do it in a roundabout way, calling your static methods from the methods
on the instance.

Of course, that isn''t what you want. Interfaces are closely tied to
objects (instances) not types (classes). While you could have interfaces
that are implemented with static properties, fields, and methods, it would
be just a roundabout way of encapsulating the functionality in an instance
(you would access static methods, which access local variables and static
fields, etc, etc).

What you should do is make your StdDeviation class a singleton class,
and expose the instance as an implementation of the Distribution interface
(which should be named IDistribution, as recommended by the public naming
conventions) through an Instance property (or GetInstance method). This
way, you have one instance which implements the interface.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<el************@web.de> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
大家好,

我还是C#的新手,所以也许有人可以向我解释一下CS0536的理由:''someClass''没有实现接口成员
''some.Interface.Member()''。 ''someClass.Member()''要么是静态的,要么是公共的,或者是错误的返回类型。

为什么我不能有一个用static实现接口的类
方法? IIRC,在Java或C ++中,通过类名(someClass :: Member())或通过
对象实例调用静态成员是可能的。
我的动机是我必须使用数学/随机
分布。因此我基本上有一个

接口分布{
双概率(双x);
}
和实现它的几个类。此外,有一些
计算方法返回分布,因此它们给我
分布的实例。好吧到目前为止:)

现在有标准的正态分布。您可能还记得您的数学课程中的
,它并不依赖于任何特定的参数,而且是常用的。所以我想在很多地方调用StdNormalDistribution :: probability()等。
此外,StdNormalDistribution is_a分布,可能由上面提到的计算方法返回。因此,如果StdNormalDistribution可以作为实例返回,那将是很好的
在需要时静态调用。

是否有一个好的在C#中解决这个问题的方法?我的oop设计坏了吗?我是否忽视了按原样这样做的一个很好的理由?

[当然是其他方式 - 使接口静态但非静态的实例无法工作;我甚至不确定静态是否静态
在界面中有意义吗?]

非常感谢,马蒂亚斯
Hi all,

I''m still new to C# so maybe someone can explain me the rationale for

CS0536: ''someClass'' does not implement interface member
''some.Interface.Member()''. ''someClass.Member()'' is either static, not
public, or has the wrong return type.

Why can''t I have a class implementing the interface with static
methods? IIRC, in Java or C++ it''s possbile to call a static member
either through the class name (someClass::Member()) or through an
object instance.

My motivation is that I have to work with mathematical/stochastical
distributions. Thus I basically have an

interface Distribution {
double probability(double x);
}

and several classes implementing it. Additionally, there are some
calculation methods that return "Distribution"s, so they give me
instances of Distributions. Allright so far :)

Now there''s the standard normal distribution. As you might remember
from your math classes, it does not depend on any specific parameters
and is commonly used. So I would like to call
StdNormalDistribution::probability() and the like in many places.
Additionally, StdNormalDistribution is_a Distribution and may be
returned by the calculation methods mentioned above. So it would be
nice if StdNormalDistribution could be returned as an instance as well
as being called statically when needed.

Is there a "nice" way to work around this in C#? Is my oop design
broken? Am I overlooking a good reason for doing it as-is?

[Of course the "other way round" - having the interface static but the
instance non-static cannot work; I''m not even sure if "static" makes
sense in an interface?]

Thanks a lot,
Matthias



Nicholas,<首先,感谢快速回复,并指出我推荐

命名约定:)。
Nicholas,

first of all, thanks for the quick reply and pointing me to recommended
naming conventions :).
你应该做的是让你的StdDeviation类成为一个单独的类,
并将实例公开为Distribution接口的实现
(应该命名为IDistribution,如公共命名
约定所推荐的那样) )通过Instance属性(或GetInstance方法)。这个方法,你有一个实现接口的实例。
What you should do is make your StdDeviation class a singleton class,
and expose the instance as an implementation of the Distribution interface
(which should be named IDistribution, as recommended by the public naming
conventions) through an Instance property (or GetInstance method). This
way, you have one instance which implements the interface.




如果我理解正确,返回IDistribution和

想要返回一个标准的正态分布返回

StdNormalDistribution.getInstance();"。


其他客户只会打电话即g。

" StdNormalDistribution.getInstance()。probability(。..)"对于他们需要的

计算。


尽管如此,这似乎是一种实现这种行为的麻烦方式。

所以,回到基本问题 - 为什么他们这样做?

它是什么?在通过实例调用静态

成员时会出现歧义的情况?


祝你好运,

Matthias



If I understand you correctly, methods that return "IDistribution"s and
want to return a standard normal distribution would "return
StdNormalDistribution.getInstance();".

Other clients would just call e. g.
"StdNormalDistribution.getInstance().probability(. ..)" for the
calculations they need.

Still, this seems to be a cumbersome way to accomplish that behaviour.
So, to come back to the basic question - why did they make it the way
it is? Any case where there would be ambiguities when calling static
members via instances?

Best regards,
Matthias


马蒂亚斯,


不,你会做这样的事情:


//分发实现。

IDistrubtion distribution = null;


//在此处设置分发实现。

//默认情况下,使用StdNormalDistribution。

distribution = StdNormalDistribution.Instance;


但是,这就是你有类工厂的原因。你有一个

枚举如下:


public enum DistributionImplementation

{

Standard,

Other1,

Other2

}


然后像这样的静态方法:


public static IDistrubition

CreateDistributionImplementation(DistributionImple mentation implementation)

{

//基于值,创建实现。

切换(实现)

{

case DistributionImplementation.Standard:

返回新的StdNormalDistribution ();


案例其他1:

返回新的Other1();

}

}


在这种情况下我不会做单身人士,在这种情况下,它有点击败了单身人士的原因。
-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< el ************ @ web.de>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...
Matthias,

No, you would just do something like this:

// The distribution implementation.
IDistrubtion distribution = null;

// Set the distribution implementation here.
// For default, use the StdNormalDistribution.
distribution = StdNormalDistribution.Instance;

However, this is why you have class factories. You would have an
enumeration like so:

public enum DistributionImplementation
{
Standard,
Other1,
Other2
}

And then a static method like this:

public static IDistrubition
CreateDistributionImplementation(DistributionImple mentation implementation)
{
// Based on the value, create the implementation.
switch (implementation)
{
case DistributionImplementation.Standard:
return new StdNormalDistribution();

case Other1:
return new Other1();
}
}

I wouldn''t make a singleton in this instance, it kind of defeats the
reason for having a singleton in this instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<el************@web.de> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
尼古拉斯,首先,感谢快速回复并指出我推荐的命名约定:)。
Nicholas,

first of all, thanks for the quick reply and pointing me to recommended
naming conventions :).
什么你应该做的是让你的StdDeviation类成为一个单独的类,
并将实例公开为Distribution
接口的实现
(应该命名为IDistribution,如公共命名所推荐的那样) /> convention)通过Instance属性(或GetInstance方法)。这个方法,你有一个实现接口的实例。
What you should do is make your StdDeviation class a singleton class,
and expose the instance as an implementation of the Distribution
interface
(which should be named IDistribution, as recommended by the public naming
conventions) through an Instance property (or GetInstance method). This
way, you have one instance which implements the interface.



如果我理解正确,返回IDistribution和
的方法想要返回标准正态分布将返回
StdNormalDistribution.getInstance();。

其他客户只会调用e。 g。
StdNormalDistribution.getInstance()。probability(。..)对于他们需要的计算。

然而,这似乎是一种麻烦的方式来实现这种行为。
所以,回到基本问题 - 为什么他们做它的方式是什么?在通过实例调用静态
成员时会出现歧义的任何情况吗?

最好的问候,
Matthias



If I understand you correctly, methods that return "IDistribution"s and
want to return a standard normal distribution would "return
StdNormalDistribution.getInstance();".

Other clients would just call e. g.
"StdNormalDistribution.getInstance().probability(. ..)" for the
calculations they need.

Still, this seems to be a cumbersome way to accomplish that behaviour.
So, to come back to the basic question - why did they make it the way
it is? Any case where there would be ambiguities when calling static
members via instances?

Best regards,
Matthias



这篇关于CS0536的基本原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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