. NET learning notes about polymorphism.

Source: Internet
Author: User
Tags abstract instance method integer
Notes assign a derived class object to a base class reference. This is a very basic part of a program that wants to process objects with polymorphism.


the so-called reference is declaring the data type.


is then instantiated. "New".


derived class references are assigned to base class references: Point = Circle


If you call the method of point at this time, because the derived class reference is assigned to the base class reference, it is a circle method

Method of
rather than point.


A syntax error occurs when a base class object is assigned to a derived class reference (without coercion type conversions).


a derived class reference is assigned to a base class reference, which can be cast by coercion, only so that the object is sent to the


the message that appears in the base class. (Represents the method and property that invokes an object)


because a derived class contains members that correspond to all the members of a base class object, the derived class may also have additional members. So,


cannot assign a reference to a base class to a derived class reference without explicit coercion of type conversions. Option Strict

The state of
on. Otherwise, additional derived class members will enter the undefined state.





does not instantiate any object's class, called an abstract class. (MustInherit)


Therefore, the sound-sending class must override the inherited abstract methods and attributes, so that the objects of those derived classes are instantiated.


For example: If someone asked you to draw a shape, what shape would you draw? Specific classes provide these details.


many good object-oriented systems use MustInherit accumulation to start with various hierarchies. Sometimes to occupy the class

Several levels at the top of the
hierarchy.

The
keyword MustOverride can declare a method and a property as abstract. Each derived class must overwrite all


MustOverride methods and properties. Using keywords (Overrides)

The difference between
MustOverride and overridable method is that the latter has a specific method to achieve. and allows derived classes to


covered. The former is not implemented, and the derived class must overwrite its methods and properties.


programming on object-oriented objects, often using iterator classes. It can facilitate all objects within a container (such as an array)


For example:


Dim arrayofshapes As Cshape () = new shape (2)


arrayofshapes (0) = Point


arrayofshapes (0) = Circle


arrayofshapes (0) = cylinder (cylindrical)


Dim shape As Cshape


for each shape in arrayofshapes


Next


Creating and Using interfaces:

The
interface specifies that the class must be an implementation of a series of public services (i.e. methods and properties).


interface to start with the keyword interface, to use the interface, the class must indicate that the implementation of their own implments


interface.


allows the interface to provide a property and method for the objects of these classes to be implemented, and returns the method or property of each object.


Delegate:


in event communications, the event sender class does not know which object or method will receive (handle) the event it raises.

What
need is a medium (or a pointer-like mechanism) between the source and the receiver.. NET Framework


defines a special type (Delegate), which provides functionality for function pointers.

A
delegate is a class that can refer to a method. Unlike other classes, a delegate class has a signature, and it can only


a reference to a method that matches its signature. In this way, the delegate is equivalent to a type-safe function pointer or a callback.

A
delegate is a reference type that refers to a Shared method of a type or an instance method of an object. In other languages,


the closest thing to a delegate is a function pointer, but a function pointer can only reference a shared function, whereas a delegate may reference a shared


and instance methods. In the latter case, the delegate stores not only the reference to the method entry point, but also the pair used to invoke the method

A reference to a
object instance.


himself understood: The essence of a delegate is the address of a method through which it can find this method and use it.


This is used in multithreaded programming.


Example:


Delegate Sub mysubdelegate (x As Integer)


Protected Sub Test ()


Dim C2 as New class2 ()


' Test the delegate.


C2. DelegateTest ()


End Sub





Class Class1


Sub Sub1 (ByVal x as Integer)


MessageBox.Show ("The value of X is:" & CStr (x))


End Sub


End Class





Class Class2


Sub delegatetest ()


Dim C1 as Class1


Dim MSD as Mysubdelegate


C1 = New Class1


' Create An instance of the delegate.


MSD = AddressOf C1. Sub1


MSD (x) ' Call to ' method.


End Sub


End Class





Delegate pass parameter: The parameters of the delegate are passed to the parameters of the Class1.sub1, as can be seen here.


There is another way, but the arguments cannot be passed.


Dim msd As New Methodinvoke (AddressOf method name)

Msd.invoke
when
call

delegates are used in conjunction with threads.























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.