开发者

When to use Inherits?

开发者 https://www.devze.com 2023-04-12 14:28 出处:网络
I am trying to build a controls object to better build my forms. Beneath, 1 object that inherits from a certain type and 1 object that is defined as a certain type.

I am trying to build a controls object to better build my forms.

Beneath, 1 object that inherits from a certain type and 1 object that is defined as a certain type.

Can you please tell me when to use ControlsCollection1 and ControlsCollection2 and why not to use a particular one. The difference between them? When do I create an object that inherits from an existing object and when do I create a object as an object of a certain type?

Second Question, Is a Collection better that a List?

I am still learning, so I hope this makes sense.

Public Class ControlsCollection1

    Private _controls As List(Of TextBox)
    Private _textbox As TextBox

    Public Sub New(ByVal textbox As TextBox)
        Me._textbox = textbox
    End Sub

    Public Property Textbox() As TextBox
        Get
            Return _textbox
        End Get
        Set(ByVal value As TextBox)
            _textbox = value
        End Set
    End Property

End Class

Public Class ControlsCollection2
    Inherits List(Of TextBox)



End Class

calling class:

Dim 开发者_JAVA技巧col1 As New ControlsCollection1(New TextBox)


Dim col2 As New ControlsCollection2

col2.Add(New TextBox)


Inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior (i.e., previously coded algorithms associated with a class) from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance. In Short (DRY) Don't Repeat Yourself principle.

A List is a Collection. It is a specialized Collection, however.

A Collection is just that: a collection of items. You can add stuff, remove stuff, iterate over stuff and query how much stuff is in there.

A List adds the information about a defined sequence of stuff to it: You can get the element at position n, you can add an element at position n, you can remove the element at position n.

In a Collection you can't do that: "the 5th element in this collection" isn't defined, because there is no defined order.

There are other specialized Collections as well, for example a Set which adds the feature that it will never contain the same element twice.

So your code makes no sense re design it after u read this and understand it... second dont add questions that are duplicates in stackoverflow Third use google alot in ur career cause it will give u fast answers. fourth Mark this as answer if u got ur answer:) goodluck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号