oops问题c# [英] oops questions c#

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

问题描述

大家好,

我希望我的基类中的方法被所有派生的

类覆盖。我的基类不能是抽象类。另外我不想

明确地为所有派生类实现一个接口。

我该怎么做?

我能有一个摘要吗?普通班级的方法?


提前致谢!!

PS

hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS

推荐答案



如果希望所有派生类都覆盖该方法,那么

类的定义是抽象的(至少在oop术语中)。


为什么你不想把它标记为抽象并声明一个抽象的方法?

你可能会用一些技巧来实现你的目标

想要将委托传递给受保护的构造函数或

接口到受保护的构造函数,但我没有看到任何理由

你想跳过那些箍以避免抽象。当你的班级真的是抽象的时候你班上的关键字




此外,有一个本质上非常抽象的类而不是

标记为摘要会让人难以维持。


HTH,


Sam

----- -------------------------------------------------- -----

我们正在招聘! B-Line Medical正在寻求Mid / Sr. .NET

开发人员在医疗产品中获得令人兴奋的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。


2006年11月28日13:27:01 -0800,parez < ps ***** @ gmail.comwrote:

If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don''t you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don''t see any reason why
you''d want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We''re hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:

>大家好,

我想在我的基类中使用一个方法被所有派生的类重写。我的基类不能是抽象类。另外我不想明确地为所有派生类实现一个接口。
我该怎么做?
我可以在常规类中使用一个抽象方法吗?

提前致谢!!
PS
>hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS


在类中有一个函数使用
$的实例b $ b本身。


例如,如果类名是somthingCollection


有一个函数定义(实现)调用getrandonsomethings(int

算)

看起来像这个

{


SomethingCollectin sc = new SomethingCollectin();


sc.add()//添加随机值。


}


Samuel R. Neff写道:
Well there is a function in the class the that uses an instance of
itself.

e.g if the class name is somthingCollection

there is a function defined (implenment) call getrandonsomethings(int
count)
which looks liek this
{

SomethingCollectin sc = new SomethingCollectin ();

sc.add( ) // add random values.

}

Samuel R. Neff wrote:

如果你想让方法被所有派生类覆盖,那么

类的定义是抽象的(至少在oop术语)。


为什么你不想把它标记为摘要并声明一个抽象的方法?

你可能会采用一些技巧来实现你想要的代理,比如将代理传递给受保护的构造函数或者

与受保护的构造函数的接口,但是我没有看到任何理由为什么你想要跳过这些箍以避免抽象。当你的班级真的是抽象的时候你班上的关键字




此外,有一个本质上非常抽象的类而不是

标记为抽象将令人困惑。


HTH,


Sam


- -------------------------------------------------- ---------

我们正在招聘! B-Line Medical正在寻求Mid / Sr. .NET

开发人员在医疗产品中获得令人兴奋的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。


2006年11月28日13:27:01 -0800,parez < ps ***** @ gmail.comwrote:
If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don''t you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don''t see any reason why
you''d want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We''re hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:

大家好,


我想要一个方法要被所有派生的

类覆盖的基类。我的基类不能是抽象类。另外我不想

明确地为所有派生类实现一个接口。

我该怎么做?

我能有一个摘要吗?普通班级的方法?


提前致谢!!

PS
hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS


parez写道:
parez wrote:

我希望我的基类中的方法被所有派生的

类覆盖。我的基础class不能是抽象类。另外我不想

明确地为所有派生类实现一个接口。

我该怎么做?

我能有一个摘要吗?普通班的方法?
I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?



不想要抽象基类的原因是什么?


假设它是合法的,一个简单的解决方案是将方法标记为基类中的虚拟方法并让它抛出一个

NotImplementedException。这个问题是你不知道直到

运行时你可能忘记覆盖派生的

类中的方法。

-

Tom Porterfield

What is the reason for not wanting an abstract base class?

Working on the assumption it is legit, a simple solution would be to mark
the method as virtual in the base class and have it throw a
NotImplementedException. The problem with this is you won''t know until
runtime that you might have forgotten to override the method in a derived
class.
--
Tom Porterfield


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

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