模块与课程? [英] module versus class?

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

问题描述




我正在开发asp.net应用程序,因此我使用VB.net。我有一些

关于最佳实践的问题。


根据我读到的关于课程和模块的内容,如果我理解正确的话,

模块与课程相同,但不能亵渎或被遗弃。 1)那是

对吗?


2)所以我猜这个模块和班级完全一样吗?


公共模块mymodule

功能MyFonction()

返回ok

结束功能

结束模块

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

Public Class myclass

共享功能MyFonction()

返回ok

结束功能

结束班级

3)什么是最好的实践:模块或课程(如果没有需要

遗产)?


4)我还读到了结构。与模块的不同之处是什么?


5)结束:(我猜不是,但可以肯定):是否可以放一个

模块,然后在asp.net应用程序的App_Code的.vb文件中(在

代码隐藏...中)?

感谢您的解释
$ b $bAndré

Hi,

i''m developping asp.net applications and therefore i use VB.net. I have some
questions about best practises.

According what i read about class and module and if i understand it right, a
module does the same as a class but cannot herite or be herited. 1)Is that
right?

2) So i guess this module does exactly the same as the class?

Public Module mymodule
Function MyFonction()
Return "ok"
End Function
End Module
------------------------------
Public Class myclass
Shared Function MyFonction()
Return "ok"
End Function
End Class
3) What''s then the best pratcise: module or class (if there is no need for
heritance)?

4) I also read about "Structure". What''s the difference with a module?

5) And for ending: (i guess not, but to be sure): is it posible to put a
module elsewhere then in a .vb file of App_Code of a asp.net application (in
code-behind ..) ?
Thanks for your explanation
André

推荐答案

关于模块或类是否更好,这真的是一个意见问题。

一旦编译完毕,它们在.NET眼中是完全相同的。

类更加面向对象,因此很多人会说它们是b $ b更好。

但是,我必须承认,当我有一个简单的项目时,模块的简单性有时会让我感到害羞。


- -

我希望这会有所帮助,

Steve C. Orr,

MCSD,MVP,CSM,ASPInsider
http://SteveOrr.net


"André&qu OT; < an@dre.tywrote in message

news:u3 ************** @ TK2MSFTNGP04.phx.gbl ...
It''s really a matter of opinion about whether modules or classes are better.
Once compiled they are identical in the eyes of .NET anyway.
Classes are more object oriented, therefore many people would say they are
"better".
However, I must admit that the simplicity of modules lures me in sometimes
when I have a simple project.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net

"André" <an@dre.tywrote in message
news:u3**************@TK2MSFTNGP04.phx.gbl...




我正在开发asp.net应用程序,因此我使用VB.net。我有一些关于最佳实践的问题。


根据我读到的关于课程和模块的内容,如果我理解正确的话,

a模块和班级一样,但不能冥想或被遗弃。 1)

对吗?


2)所以我觉得这个模块跟班级完全一样吗?


公共模块mymodule

功能MyFonction()

返回ok

结束功能

结束模块

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

Public Class myclass

共享功能MyFonction()

返回ok

结束功能

结束班级


3)什么是最好的实践:模块或课程(如果不需要

遗产)?


4)我还读到了结构。与模块的不同之处是什么?


5)结束:(我猜不是,但可以肯定):是否可以放一个

模块在其他地方然后在asp.net应用程序的App_Code的.vb文件中

(代码隐藏...)?


谢谢你你的解释
$ b $bAndré

Hi,

i''m developping asp.net applications and therefore i use VB.net. I have
some questions about best practises.

According what i read about class and module and if i understand it right,
a module does the same as a class but cannot herite or be herited. 1)Is
that right?

2) So i guess this module does exactly the same as the class?

Public Module mymodule
Function MyFonction()
Return "ok"
End Function
End Module
------------------------------
Public Class myclass
Shared Function MyFonction()
Return "ok"
End Function
End Class
3) What''s then the best pratcise: module or class (if there is no need for
heritance)?

4) I also read about "Structure". What''s the difference with a module?

5) And for ending: (i guess not, but to be sure): is it posible to put a
module elsewhere then in a .vb file of App_Code of a asp.net application
(in code-behind ..) ?
Thanks for your explanation
André




我的经验法则是


这是一个VB6宿醉吗?


和一种方式来回答

" C#有一个对应部分。

话虽如此,我不会使用模块。


公共类myclass


private sub New

''隐藏构造函数如果我想保证没有实例化

end Sub


共享功能MyFonction()

返回ok

结束功能

结束班级



André < an@dre.tywrote in message

news:u3 ************** @ TK2MSFTNGP04.phx.gbl ...

My rule of thumb is

"Is this a VB6 hangover?"

and one way to kinda answer that is
"does C# have a counter part".
Having said that, I wouldn''t use Modules.

Public Class myclass

private sub New
''hide the constructor if I want to guarantee no instantiation
end Sub

Shared Function MyFonction()
Return "ok"
End Function
End Class


"André" <an@dre.tywrote in message
news:u3**************@TK2MSFTNGP04.phx.gbl...




我正在开发asp.net应用程序,因此我使用VB.net。我有
Hi,

i''m developping asp.net applications and therefore i use VB.net. I have



一些

some


关于最佳实践的问题。


根据我读到的关于课程和模块的内容,如果我理解正确,
questions about best practises.

According what i read about class and module and if i understand it right,



a

a


module和班级一样,但不能亵渎或被遗弃。 1)那是

对吗?


2)所以我猜这个模块和班级完全一样吗?


公共模块mymodule

功能MyFonction()

返回ok

结束功能

结束模块

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

Public Class myclass

共享功能MyFonction()

返回ok

结束功能

结束班级


3)什么是最好的实践:模块或课程(如果不需要

遗产)?


4)我还读到了结构。与模块的不同之处是什么?


5)结束:(我猜不是,但可以肯定):是否可以放一个

模块,然后在asp.net应用程序的App_Code的.vb文件中
module does the same as a class but cannot herite or be herited. 1)Is that
right?

2) So i guess this module does exactly the same as the class?

Public Module mymodule
Function MyFonction()
Return "ok"
End Function
End Module
------------------------------
Public Class myclass
Shared Function MyFonction()
Return "ok"
End Function
End Class
3) What''s then the best pratcise: module or class (if there is no need for
heritance)?

4) I also read about "Structure". What''s the difference with a module?

5) And for ending: (i guess not, but to be sure): is it posible to put a
module elsewhere then in a .vb file of App_Code of a asp.net application




(in


代码隐藏..)?


感谢您的解释
$ b $bAndré

code-behind ..) ?
Thanks for your explanation
André


3月19日上午11:49,André < a ... @ dre.tywrote:
On Mar 19, 11:49 am, "André" <a...@dre.tywrote:




我正在开发asp.net应用程序因此我使用VB.net。我有一些

关于最佳实践的问题。


根据我读到的关于课程和模块的内容,如果我理解正确的话,

模块与课程相同,但不能亵渎或被遗弃。 1)那是

对吗?


2)所以我猜这个模块和班级完全一样吗?


公共模块mymodule

功能MyFonction()

返回ok

结束功能

结束模块

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

Public Class myclass

共享功能MyFonction()

返回ok

结束功能

结束班级


3)什么是最好的实践:模块或课程(如果没有需要

遗产)?


4)我还读到了结构。与模块的不同之处是什么?


5)结束:(我猜不是,但可以肯定):是否可以放一个

模块,然后在asp.net应用程序的App_Code的.vb文件中(在

代码隐藏...中)?


谢谢你你的解释
$ b $bAndré
Hi,

i''m developping asp.net applications and therefore i use VB.net. I have some
questions about best practises.

According what i read about class and module and if i understand it right, a
module does the same as a class but cannot herite or be herited. 1)Is that
right?

2) So i guess this module does exactly the same as the class?

Public Module mymodule
Function MyFonction()
Return "ok"
End Function
End Module
------------------------------
Public Class myclass
Shared Function MyFonction()
Return "ok"
End Function
End Class

3) What''s then the best pratcise: module or class (if there is no need for
heritance)?

4) I also read about "Structure". What''s the difference with a module?

5) And for ending: (i guess not, but to be sure): is it posible to put a
module elsewhere then in a .vb file of App_Code of a asp.net application (in
code-behind ..) ?

Thanks for your explanation
André



这里考虑模块时要考虑的事项:


您的应用程序可能会增大。事实上,这是一个虚拟的确定的b
。项目增长。模块的问题在于它们中的

函数有点像全局变量。你会倾向于

有很多,你永远不知道他们在哪里宣布了b $ b。从维护的角度来看,它们很快就会变得难以管理。


在我的代码中,我可以在模块中引用一个函数而不需要

指定它所在的模块。例如,考虑以下




模块GlobalStuff

Public Function GetCircumference(ByVal radius As Single)As Single

''代码遵循

End Sub

结束模块


在你的代码的某个地方,我只需要调用

即可调用该函数:


Dim r As Single = 1237.264

Dim c As Single = GetCircumference(r)

首先,当我的项目很小时,这就是蛋糕。我知道定义了

函数的位置。但随着它的大小不断增长,像这样的东西变得如此明显。当然,我可以把鼠标指针放在上面然后点击

F2,但我为什么要这么做?


基本的最佳做法是软件应该是自我记录。

当然,我可以用模块名称作为前缀,如下所示:


Dim r As Single = 1237.264

Dim c As Single = GlobalStuff.GetCircumference(r)

但GlobalStuff.GetCircumference并没有告诉我任何超过

的事实我有一堆不相关的功能。在

另一方面,如果函数具有很强的内聚性(即与b $ b相关的紧密相关)函数,那么名称应该像Circle一样:


Dim r As Single = 1237.264

Dim c As Single = Circle.GetCircumference(r)


在这种情况下,不会''我还真想要一堂课吗?如果我这样做,为什么

我会让编译器为我做这个?我是否相信那些来自我身边的人会知道模块之间的差异(那个'b
编译成一个不可能的类继承了什么,但共享

成员)和一个班级?

如果我以后决定我*希望*派生出来会怎么样? />
来自该功能?要扩大它吗?隐藏细节?

如果我想将它与

模块中的其他功能分开怎么办?


In我的想法,这纯粹是我的思维方式,你可以随意使用

以不同的方式思考,模块鼓励你把功能集中在一起

与之无关彼此。这与模块来自的

有很大关系。他们的根源在于Basic的旧时代,

课程出现之前。


另一方面,课程鼓励你从对象的角度思考

以及可以对它们执行的操作。你倾向于用

来隐藏其内部细节。总而言之,这是一种优秀的做事方式,因为它产生的代码可以更容易维护和调试。 (我的意思是倾向于;

你仍然可以编写令人痛苦的课程。)


所以最后,我的建议是尽可能避免使用模块。使用

课程。当然,编译器会为你制作一个,但作为一项规则,我会尽量不让编译器为我做出决定,如果我得到了

选择明确地做。它让我的意图变得清晰,特别是对于那些必须来到我身后并接管我的

代码的下一个可怜的混蛋。


迈克

Here''s something to consider when thinking about modules:

Your application will likely grow in size. In fact, it''s a virtual
certainty. Projects grow. The problem with Modules is that the
functions in them are kind of like global variables. You''ll tend to
have lots of them, and you''ll never really know where they''re
declared. From a maintenance standpoint, they get unmanageable pretty
quickly.

In my code, I get to reference a function in a module without having
to specify the module that it''s in. For example, consider the
following:

Module GlobalStuff
Public Function GetCircumference(ByVal radius As Single) As Single
'' Code to follow
End Sub
End Module

Somewhere in your code, I get to invoke that function by just calling
it like this:

Dim r As Single = 1237.264
Dim c As Single = GetCircumference(r)

At first, when my project is small, this is cake. I know where that
function is defined. But as it grows in size, things like that don''t
become so obvious. Sure, I can put the mouse pointer on it and click
F2, but why should I have to?

A basic best practice is that software should be self-documenting.
Sure, I could prefix the call with the module name, like this:

Dim r As Single = 1237.264
Dim c As Single = GlobalStuff.GetCircumference(r)

But GlobalStuff.GetCircumference doesn''t tell me anything more than
the fact that I have a homogenous clump of unrelated functions. On the
other hand, if the functions are highly cohesive (that is closely
related) functions, then the name should be something like Circle:

Dim r As Single = 1237.264
Dim c As Single = Circle.GetCircumference(r)

In that case, wouldn''t I really want a class anyway? And if I do, why
would I let the compiler do it for me? Do I trust someone who''s coming
along behind me to know the difference between a module (that''s
compiled into a class that can''t be inherited with nothing but shared
members) and a class?

And what happens if I decide at a later date that I *want* to derive
from that functionality? To expand on it? To hide away the details?
What if I want to separate it from the other functions in that
module?

In my mind, and this is purely my way of thinking and you''re free to
think differently, modules encourage you to lump functions together
that have NOTHING to do with each other. That has a lot to do with
where modules come from. Their roots lie in the old days of Basic,
before classes came around.

Classes, on the other hand, encourage you to think in terms of objects
and the operations that can be performed on them. You tend to think in
terms of hiding away their internal details. All in all, it''s a
generally superior way of doing things, because it produces code that
tends to be easier to maintain and debug. (And I mean "tends to be;"
you can still write classes that are a pain in the neck to work with.)

So, in the end, my suggestion is to avoid modules if you can. Go with
classes. Sure, the compiler will make one for you, but as a rule, I
try not to let the compiler make a decision for me if I''m given the
choice to do it explicitly. It makes my intent clear, especially to
The Next Poor Bastard who has to come along behind me and take over my
code.

Mike


这篇关于模块与课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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