OOP在vb.net上有疑问吗? [英] OOP Question in vb.net ?

查看:85
本文介绍了OOP在vb.net上有疑问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有2个简单的类:事件和BMD.这是代码:

活动:

Hi all,

I have 2 simple classes : Events and BMD. And here is the code :

Events:

Public Class Events
    Implements IEvents

    Private bmdd As BMD

    Public Sub New(ByRef master As Object)
        Me.bmdd = master
    End Sub

    Public Function getevents1() As String Implements IEvents.getevents
        Return ""
    End Function
End Class



在BMD中,我具有此属性,其中_eventfield是事件类型的对象:



in the BMD i have this property where _eventfield is object of type Events :

Public Property _eventproperty() As IEvents Implements IBMD._eventproperty
       Get
           If _eventfield Is Nothing Then
               _eventfield = New Events(Me)
           End If
           Return _eventfield
       End Get
       Set(ByVal value As IEvents)
           If _eventfield Is Nothing Then
               _eventfield = New Events(Me)
           End If
       End Set
   End Property



所以当我这样做:



So when i do :

_eventfield = New Events(Me)



它正在调用Events的构造函数.

所以我的问题是



It is calling the constructor of Events.

So my question is what does

Me.BMD= master

是什么意思?

谢谢.

means ?

Thanks.

推荐答案

它为变量分配对象...但是,从未使用过该变量,该变量的类型为BMD,而分配给它的对象为类型的对象(可能是也可能不是BMD类型).我建议打开严格启用选项 [面向对象编程概念(OOP)简介)和更多 [ ^ ]
但是,也许可以通过在VB.NET中获取一些有关编程的初学者书籍或文章来获得更多帮助.例如这一个 [ ^ ].
It assigns an Object to a variable... However, the variable is never used, the variable is of type BMD while the object assigned to it is of the type Object (which may or may not be of the type BMD). I suggest turning Option Strict On[^] and see the error appearing! A GetEvents that returns a String is peculiar to say the least. This piece of code is just strange and has actually nothing to do with OOP... Assigning values to variables is the absolute basics. For more information on OOP I suggest reading this: Introduction to Object Oriented Programming Concepts (OOP) and More[^]
But perhaps you''d be more helped by grabbing some beginner books or articles on programming in VB.NET... Such as this one[^].


首先,Me意味着对对象的引用作为隐式参数隐式传递给实例(非静态)方法,以允许该方法评估实例.换句话说,假设您拥有以下内容:
First of all, Me means the reference to the object implicitly passed as a hidden parameter to an instance (non-static) method in order to allow this method to assess the instance. In other words, suppose you have this:
myInstance.MyMethod(someParameter);


这等效于以下伪代码:


This is equivalent to this pseudo-code:

TheClassOfMyInstance.MyMethod(myInstance, someParameter);


此伪代码可用作真实代码,通过静态方法MyMethod演示对即时方法的仿真.

现在,赋值me.bmdd = master是错误的,因为masterObject,但是bmddBMD.您不能将较专门的实例分配给较不专门的实例,因为BMD的成员要多于Object,并且尝试访问其中一些额外成员的尝试将导致致命错误.幸运的是,该系统不会让您走得那么远.如果通过强制转换为BMD来强制执行此分配,则在master实例不是运行时类型或不是从BMD类型派生的所有情况下,强制转换本身都会引发异常类型.

请注意,区分编译类型名称和运行时名称非常重要.

我同意Naerling关于学习一些编程和语言的知识.

硬编码任何立即常量(尤其是字符串)是非常糟糕的. string.Empty甚至代替".

实际上,由于上​​述原因,构造函数New的签名没有任何意义.您可以使用Public Sub New(ByRef master As BMD).在注释中,您说实际参数的类型为Events.如果是这样,构造函数将永远无法工作.如果使用签名Public Sub New(ByRef master As Events),这将毫无意义.

一些注意事项:

以下名称违反了非常有用的Microsoft命名约定:Events(类名称不能为复数),_eventfield(请勿使用下划线),getevents1(您应使用驼峰式表示方法名称,而不是数字) .

—SA


This pseudo-code could be used as a real code demonstrating simulation of an instant method by a static version of the method MyMethod.

Now, the assignment me.bmdd = master is wrong, because master is Object, but bmdd is BMD. You cannot assign more specialized instance to less specialized, because BMD has more members then Object and an attempt to access some of those extra members would cause a fatal error. Fortunately, the system won''t let you go that far. If you force this assignment by casting to BMD, the cast itself will trow exception in all cases where the instance of master is not of the run-time type BMD or derived from this type.

Note that it is important to distinguish compile-type name and run-time name.

I agree with Naerling about learning some programming and language.

It''s very bad to hard-code any immediate constants, especially strings; even instead of "" should be string.Empty.

Actually, the signature of the constructor New makes no sense due to the reason explained above. You could use Public Sub New(ByRef master As BMD). In comments you say that the actual parameter is of the type Events. If so, the constructor would never work at all. And if you used the signature Public Sub New(ByRef master As Events) it would make no sense.

A couple of notes:

The following names violate very useful Microsoft naming conventions: Events (class name can not be plural), _eventfield (don''t use underscore), getevents1 (you should use camel case for method names, not numerals).

—SA


这篇关于OOP在vb.net上有疑问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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