模块和类之间有什么区别? [英] What is the difference between a module and a class?
问题描述
在Visual Studio中将新文件添加到VB.Net项目时,可以同时选择类"和模块".一个类被描述为
When adding a new file to a VB.Net project in Visual Studio, I'm given the option of both a 'Class' and a 'Module'. A class is described as
An empty class file
模块描述为
A file for storing groups of functions
这似乎暗示着模块不如类有用,因为类可以存储更多的功能组.
This seems to imply that a module is less useful that a class, since a class can store groups of functions and more.
是模块只是一组功能,还是模块的功能超出Visual Studio文档的建议?
Is it the case that a module is simply a group of functions, or is there more to the module than the visual studio documentation suggests?
推荐答案
class 是一种类型.您可以像使用其他任何类型(String
,Integer
,Date
,FileInfo
...)一样使用此类型来声明变量,参数,属性和函数返回类型.
A class is a type. You can use this type like any other type (String
, Integer
, Date
, FileInfo
...) to declare variables, parameters, properties and function return types.
让我们举个例子:
Public Class Person
Public Property FirstName As String
Public Property LastName As String
Public Overridable Sub Print() 'I'll explain Overridable later.
Console.WriteLine("{0} {1}", FirstName, LastName)
End Sub
End Class
现在您可以声明Person
Dim sue, pete As Person
Dim persons As List(Of Person)
sue = New Person()
sue.FirstName = "Susan"
sue.LastName = "Miller"
pete = New Person()
pete.FirstName = "Peter"
pete.LastName = "Smith"
persons = new List(Of Person)()
persons.Add(sue)
persons.Add(pete)
For Each person As Person In persons
person.Print()
Next
模块是静态的. IE.存储在模块中的数据仅存在一次.另一方面,您不必使用New
实例化模块,因此它们通常用于存储全局数据和用于全局可用的方法.例如,您可以将人员列表存储在模块中.
Whereas modules are static. I.e. Data stored in a module exists exactly once. On the other hand, you don't have to instantiate a module with New
, therefore they are often used to store global data and for methods that are available globally. For instance you could store the persons list in a module.
但是您可以使用类做更多的事情.您可以从基类派生一个类.这个新类继承了基类的所有内容,并且可以向其添加更多内容.例如,您可以从Person
But there is much more you can do with classes. You can derive a class from a base class. This new class inherits everything from the base class and can add more stuff to it. For instance you could derive an Employee
class from Person
Public Class Employee
Inherits Person
Public Property Salary As Decimal
Public Overrides Sub Print
Console.WriteLine("{0} {1}, Salary = {2}", FirstName, LastName, Salary)
End Sub
End Class
Person.Print
中的Overridable
关键字允许派生类重新定义(覆盖)Print
方法. (类中的函数和Subs称为方法.)
The Overridable
keyword in Person.Print
allows deriving classes to re-define (to override) the Print
method. (Functions and Subs in classes are called methods.)
员工的工作分配与人员"兼容.您可以将雇员添加到persons
列表中.不需要在For Each循环中进行任何更改,即,调用person.Print()
会自动调用正确的Print
方法(第一个用于正常"人员,第二个用于员工).
Employees are assignment compatible to Persons. You could add an employee to the persons
list. This does not require any change in the For Each loop, i.e., the call of person.Print()
automatically calls the right Print
method (the first one for "normal" persons and the second one for employees).
Dim emp as Employee
emp = New Employee()
emp.FirstName = "Frank"
emp.LastName = "Taggart"
emp.Salary = 3500.00D
persons.Add(emp)
关于类还有很多要说的.我希望您对使用类可以有所了解.
There is much more to say about classes. I hope that you got a certain idea of what you can do with classes.
请参见 Visual Basic中的对象和类,尤其是 查看全文