理解类 [英] Understanding Classes

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

问题描述

虽然我已经设法完成了我的编程,但我想了解更多并了解我正在做的事情。我过去根据我阅读过的文档创建了类,但我并不完全理解它们,所以我希望你们中的一位知识渊博的专家可以用非技术术语来解释。

Although I have managed to get by with my programming, I would like to know more and understand more of what I am doing. I have created Classes in the past based on documentation I have read but I don't fully understand them so, I'm hoping one of you more knowledgeable experts might be able to explain in a non technical term.

目前在我的课程中,我放置了一个New Sub,我知道每次调用该类时都会调用它。

currently in my classes, I place a New Sub which I know gets called every time the class is called.

在一个模块中,我可以输入

in a module, I could type

Public MyVar As String = String.Empty



in a class,文档显示创建属性


In a class, Documentation shows to create properties

Private MyVar As String

    Property _Var() As String
        Get
           Return MyVar
        End Get
        Set(ByVal Value As String)
            MyVar = Value
        End Set
    End Property

这是额外的代码,这对模块中的简单1行代码声明有什么好处?

This is extra code, what is the benefit of this vs a simple 1 line code declaration in a module?

我也可以写一个像这样的属性

I can also write a property like this

Private Property MyVar As String = String.Empty

它仍然是一个属性,那么一种属性方法与另一种属性方法的区别/优势是什么?

Its still a property so what is the difference / advantage of one property method vs the other?

什么是覆盖?

什么是重载?

何时最好使用Class而不是模块?

When is it best to use a Class instead of a Module?

在我当前的家庭项目中,我在表单加载时创建了一堆位图,矩形和整数。我从数据库中收集一些数据并将该数据应用于整数。这仅限于9位图等。如果我把它变成了一个类,那么这是否意味着
可以创建无限量,因为每个New Class调用都是一个新的位图实例?

In my current home project, I am creating a bunch of Bitmaps, rectangles and integers when my form loads. I collect some data from a db and apply that data to the integers. This is limited to 9 bitmaps etc. If I made it into a class instead, would that mean its possible to create an unlimited amount because each New Class call is a new instance of bitmap?

对于问题清单,我只能节省时间(工作,家庭等),我希望进一步了解。如果有人可以花时间用一个简单的术语来解释,我可以通过这种方式学到更多。 MSDN文档我觉得你将
发送到多个页面,每个页面都提供了一个难题的一部分....

Sorry for the list of questions, I only get limited time on my programming (Work, Family etc) and I do wish to further my knowledge. If someone could take the time to explain in a simple term, I can learn much easier that way. MSDN documentation I feel sends you to multiple pages with each page providing a part of the puzzle....

感谢任何评论

Nacho

纳乔是奈杰尔的衍生物 - 真实的事实!我是VB.Net的自学者。 50%的时间,我100%正确!

Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

推荐答案


这是额外的代码,这对模块中的简单1行代码声明有什么好处?

This is extra code, what is the benefit of this vs a simple 1 line code declaration in a module?

使用类和模块,你试图比较两个完全不同的东西为不同的目的做不同的工作,所以不可能只列出差异。

With classes and modules you are trying to compare two quite different things that do different jobs for different purposes, so it isn't really possible to just list the differences.

使用Property获得的主要好处是,因为属性Get和property设置代码是一种方法,然后您可以包含额外的代码,这些代码将在Set和Get发生时执行。 例如,您可能希望在Set例程中包含验证
代码,以确保永远不会将该属性设置为无效值。  或者,当Get发生时,结果可能是基于当时存在的其他值的计算,而不是仅返回变量值。 
尽管该功能并未用于许多属性,但程序员倾向于使用属性而不是字段,因为如果将来需求发生变化,选项就会存在。

The main benefit you get from using a Property is that, because the property Get and property Set code is a method, then you can include extra code which will be executed whenever the Set and Get occurs.  For instance, you might want to include validation code in a Set routine, so that you ensure the property can never be set to an invalid value.   Or when the Get occurs the result might be a calculation, based on other values as they exist at that moment, rather than just returning a variable value.  Even though that feature is not used for many properties, programmers tend to use properties rather than fields because the option is there in case requirements change in the future.

你可以开始通过整理术语:

    https://vbdotnetblog.wordpress.com/overview/oop-terminology/

You can start by getting the terminology sorted out:
    https://vbdotnetblog.wordpress.com/overview/oop-terminology/

并查看使用该起点的MS文档。

and check out the MS documentation using that starting point.


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

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