c#接口限制??? [英] c # interfaces limitations ???

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

问题描述

嘿所有


这是我的问题考虑两个班级


A类:SomeSystemClassA


B类:SomeSystemClassB

现在我必须为A类和B类添加一个通用接口。

方式A和B派生我无法添加一个共同点抽象基类为

他们。


所以我这样做


A类:SomeSystemClassA,ICommonInterface

B类:SomeSystemClassB,ICommonInterface


我面临的问题是C#不允许接口有

静态函数或任何函数实现(除非

我遗漏了什么)


在这样的场景中,如果ICommonInterface有10个函数,则机会是A

和B包含3个功能完全相同导致代码

重复A和B


怎么会出现这个问题要解决?有没有比这种方法更好的方法?


在ICommonInterface上有一个CommonInterfaceImplClass属性和

类A和B有所有方法将调用发送到

CommonInterfaceImplClass。


A级

{

Void CommonFunc1()

{

CommonInterfaceImplClass。 CommonFunc1()

}

}

B级

{

Void CommonFunc1 ()

{

CommonInterfaceImplClass。 CommonFunc1()

}

}

仍然代码重复但不是维护噩梦。

解决方案

接口不是C#概念,它们是面向对象的原则。


接口纯粹是抽象的。也就是说,它们不包含任何

实现或数据成员。


在您的情况下,我会执行以下操作:

IAnimal(接口)

- 具体的

实现对象所需的所有函数声明都在这里


哺乳动物:IAnimal(抽象类)

- 如果两个孩子之间有共享功能,他们就会在这里实施
...

猫:哺乳动物(具体课程)

狗:哺乳动物(具体课程)

- 使用在哺乳动物中发现的功能,但有自己的实例化。 />
希望有所帮助。


Daniel。


< ra ****** @ hotmail.com>在消息中写道

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

嘿所有

这是我的问题考虑两个班级

A类:SomeSystemClassA

B类:SomeSystemClassB

现在我必须为A类和B类添加一个通用接口。
方式A和B派生我不能为它们添加一个共同的抽象基类。

所以我做了A:SomeSystemClassA,ICommonInterface

B类:SomeSystemClassB,ICommonInterface

我面临的问题是C#没有不允许接口具有静态功能或任何功能实现(除非
我遗漏了什么)

在这样的场景中,如果ICommonInterface有10个功能机会是A
和B包含3个完全相同的功能导致代码在A和B中重复

如何解决这个问题?有没有比这种方法更好的方法?

在ICommonInterface上有一个CommonInterfaceImplClass属性,在A和B类中,所有方法都将调用调用到CommonInterfaceImplClass。

A类
{无效CommonFunc1()
{CommonInterfaceImplClass。 CommonFunc1()
}
}

B级
{无效CommonFunc1()
{
CommonInterfaceImplClass。 CommonFunc1()
}
}

仍然代码重复,但不是维护噩梦。



静态接口上的方法实际上没有意义。接口是一个

合同,实现者必须支持。


在你的场景中,你不能使用实现继承来提供

常用方法的功能,您可以创建一个单独的

类来提供此功能。然后,实现您的

接口的每个类都可以拥有此类的私有实例。接口

实现的方法可以调用这个类。


HTH

Dan


" ra ****** @ hotmail.com"写道:

嘿所有

这是我的问题考虑两个班级

A类:SomeSystemClassA
B类:SomeSystemClassB

现在我必须为A类和B类添加一个通用接口。
A和B的派生方式我不能为他们。

所以我做了。

A类:SomeSystemClassA,ICommonInterface

B类:SomeSystemClassB,ICommonInterface
我遗漏了什么)

在这样的场景中,如果ICommonInterface有10个功能,则A
和B包含3个完全相同的功能,导致代码在A和B中重复

这怎么可能问题要解决?有没有比这种方法更好的方法?

在ICommonInterface上有一个CommonInterfaceImplClass属性,在A和B类中,所有方法都将调用调用到CommonInterfaceImplClass。

A类
{无效CommonFunc1()
{CommonInterfaceImplClass。 CommonFunc1()
}
}

B级
{无效CommonFunc1()
{
CommonInterfaceImplClass。 CommonFunc1()
}
}

仍然代码重复但不是维护噩梦。



>在您的场景中,您没有使用实现继承来

提供常用方法的功能,您可以创建一个单独的类来提供此功能。然后,实现您的
接口的每个类都可以拥有此类的私有实例。接口
实现的方法然后可以调用这个类。




虽然这没有错。这些类具有共同代码的事实

表明它是与该对象有关的东西。如果是这种情况,那么面向对象的方式就是将代码重构出来,并将

放入作为这些对象的子集的类中。因此创建一个

中间层,为具体类提供父母,但是

接口的子项。


创建全局类倾倒地并不是一种干净或清晰的方式来展示你想要做的事情。虽然一些兄弟姐妹使用的几种方法不会影响这种情况,但是每次你想要进入

的地方都很容易引用一些常见的代码,它会进入一些大的

类。



Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn''t allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.

解决方案

Interfaces are not a C# concept, they''re an Object Oriented principle.

Interfaces are purely abstract. That is they do not contain any
implementation or data members.

In your case I''d do the following:

IAnimal (interface)
- all the function declarations that are required for a concrete
implementation of the object go here

Mammal : IAnimal (abstract class)
- if there are shared functions between two children they are
implemented here...

Cat : Mammal (concrete class)
Dog : Mammal (concrete class)
- uses functions found in Mammal, but has its own instantiations.
Hope that helps.

Daniel.


<ra******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn''t allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.



Static methods on an interface would not really make sense. An interface is a
contract, which the implementor must support.

In your scenario, where you acnnot use implementation inheritance to provide
the functionality for common methods, you could instead create a separate
class which provides this functionality. Each class that implements your
interface could then have a private instance of this class. The interface
implemented methods could then call into this class.

HTH
Dan

"ra******@hotmail.com" wrote:

Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn''t allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.



> In your scenario, where you acnnot use implementation inheritance to

provide
the functionality for common methods, you could instead create a separate
class which provides this functionality. Each class that implements your
interface could then have a private instance of this class. The interface
implemented methods could then call into this class.



While this is not wrong. The fact that the classes have code in common
suggests it to be something pertaining to that object. If this is the case,
then the object orientated way would be to have the code refactored out, and
placed into a class that is a sub set of these objects. Hence creating an
intermediate layer, parenting the concrete classes, but a child of the
interface.

Creating a global class dumping ground isn''t a clean or clear way of showing
what you''re trying to do. While a few methods used by a couple of sibling
classes doesn''t donote this scenario, it''s an easy habit to get into where
every time you want to reference some common code, it goes into some large
class.



这篇关于c#接口限制???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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