Comparison of various language Polymorphism

Source: Internet
Author: User
Tags c constructor modifiers

The content related to various language polymorphism and some advanced features are summarized.

-------------------------
Delphi
-------------------------
[Heavy Load]
Add overload

[Virtual base class]
Delphi does not support multi-inheritance, so there is no virtual base class.

[Virtual function]
There are two methods to define the virtual function of the parent class:
(1) procedure Draw; virtual;
(2) procedure Draw; dynamic;
The two are basically the same, the former is faster, and the latter is better.

The subclass can be overwritten with override:
Procedure Draw; override;

Pure virtual functions]
The parent class is defined as follows:
Procedure fly (); virtual; abstract;

Subclass uses the following:
Procedure fly (); override;

[Abstract class]
It is not clear whether DELPHI has this concept. It does not seem to have been mentioned in the impression.

[Interface]
Ifoo = interface
['{20177bf60-aa33-11d0-a9bf-9a4537a42701}']
Function F1: integer;
End;

Ibar = interface
['{20177bf61-aa33-11d0-a9bf-9a4537a42701}']
Function F1: interger;
End;

[Attribute]
Tmyobject = Class
PRIVATE:
Somevalue: integer;
Procedure setsomevalue (avalue: integer );
Public:
Property Value: integer read somevalue write setsomevalue;
End;

Procedure tmyobject. setsomevalue (avalue: integer );
Begin
If somevalue <> avalue then
Somevalue: = avalue;
End;

----------------------
C ++
----------------------
[Heavy Load]
Define functions with different parameters of the same name. No special keywords are available.

[Virtual base class]
To avoid multiple copies of similar members in the base class, use the virtual keyword to declare the inheritance method.
Class A // defines the Base class
{
A (int I) {}// base class constructor, with A parameter
...
};
ClassB: virtual public A // A serves as the virtual base class of B
{
B (int n): A (n) {}// Class B constructor. initialize the virtual base class in the initialization table.
...
};
ClassC: virtual public A // A serves as the virtual base class of C.
{
C (int n): A (n) {}// Class C constructor. initialize the virtual base class in the initialization table.
...
};
Classd: publicb, publicc // constructor of class D, which initializes all base classes in the initialization table.
{
D (int n): A (N), B (n), C (n ){}
...
}

[Virtual function]
Declare the function with virtual in the parent class and implement the function definition;
This function can be reloaded in the subclass to implement dynamic Association.
The parent class contains virtual functions:
Class Point
{
Public:
Point (double I, Double J) {x = I; y = J ;}
Virtual double area () const {return 0.0 ;}
PRIVATE:
Double X, Y;
};

Override the virtual function in the subclass:
Class rectangle: Public point
{
Public:
Rectangle (double I, Double J, Double K, double 1 );
Virtual double area () const {return w * h ;}
PRIVATE:
Double W, h;
};

Call this function to implement dynamic Association:
Void fun (point & S)
{
Cout <S. Area () <Endl;
}

Pure virtual functions]
A function declared with the keyword virtual, and does not implement the function definition.
For example, the following class defines two pure virtual functions,
Class Point
{
Public:
Point (INT I = 0, Int J = 0) {x0 = I; Y0 = J ;}
Virtual void set () = 0;
Virtual void draw () = 0;
Protected:
Int x0, y0;
};

[Abstract class]
Classes with pure virtual functions are called abstract classes and can only be used as base classes.
An abstract class is a special class that is created for the purpose of abstraction and design. It is at the upper level of the hierarchy of inheritance. An abstract class cannot define objects, in practice, to emphasize that a class is an abstract class, you can describe the constructor of this class as a protected access control permission.
Class shape
{
Public:
Virtual float area () const {return 0.0 ;}
Virtual float volume () const {return 0.0 ;}
Virtual void shapeName () const = 0;
};

If the function in the parent class is not overloaded in the subclass, The subclass is also an abstract class.

[Interface]
The form is to replace the class in the abstract class with the interface, which can be inherited from other interfaces. Of course, it is most important to be inherited by other classes.
For example, the IUnknown interface commonly used in COM is declared as follows:
Interface IUnknown {
Virtual HRESULT STDMETHODCALLTYPE
QueryInterface (REFIID riid, void ** GMM) = 0;
Virtual ulong stdmethodcalltype AddRef (void) = 0;
Virtual ulond stdmethodcalltype Release (vod) = 0;
};
In C ++, abstract classes are generally used to implement interfaces.

[Attribute]
Standard C ++ does not have this stuff.

--------------------------
C #
--------------------------
[Heavy Load]
Define a function with different parameters of the same name. There is no special keyword, just like C ++.

[Virtual base class]
This is not popular under single inheritance.

[Virtual function]
Declare the function with virtual in the parent class and implement the function definition;
Public virtual bool SQLExe (bool B)
{
Return true;
}
Override is used in the subclass to overwrite this function, which enables Dynamic Association.

Pure virtual functions]
The function declared with the keyword abstract does not implement the definition of the function, and must exist in the abstract class.
Public abstract bool f ();
Override is used in the subclass to override this function.

[Abstract class]
Abstract Declaration: the parent class can contain virtual function declaration, which does not implement the definition of virtual function. It can only be used as a base class.
Public abstract class cl
{
Public cl (){}
Public abstract bool f ();
}

[Interface]
Similar to class definition, use interface description.
[Attributes] [modifiers] interface identifier [: base-list] {interface-body} [;]
1. attributes (optional): additional definition information.
2. modifiers (optional): allowed modifiers include new and four access modifiers.
New, public, protected, internal, and private.
In an interface definition, the same modifier is not allowed to appear multiple times. The new modifier can only appear in the nested interface, indicating that the inherited members with the same name are overwritten.
The public, protected, internal, and private modifiers define access permissions for interfaces.
3. Indicators and events.
4. identifier: interface name.
5. base-list (optional): contains a list of one or more explicit base interfaces separated by commas.
6. interface-body: defines interface members.
7. An interface can be a member of a namespace or class and contain the signature of the following members: method, attribute, and indexer.
8. An interface can be inherited from one or more basic interfaces.

Multiple interfaces are allowed.

For example, an interface with an event is defined as follows:
Public delegate void StringListEvent (IStringList sender );
Public interface IStringList
{
Void Add (string s );
Int Count {get ;}
Event StringListEvent Changed;
String this [int index] {get; set ;}
}

[Attribute]
Private int myheight;

Public int MyHeight
{
Get {
Return this. myheight;
}
Set
{
This. myheight = value;
}
}

--------------------------
JAVA
--------------------------

[Heavy Load]
The function can have different parameters with the same name.

[Virtual base class]
Single inheritance is not popular!

[Virtual function]
Without this concept, java automatically implements Dynamic Association. If you don't believe it, try it!

Pure virtual functions]
In JAVA, it is called an abstract function, not a pure virtual function. It must be defined in an abstract class and there is no method body. For example:
Abstract class Demo {
Abstract void method1 ();
Abstract void method2 ();
...
}

[Abstract class]
Abstract class Demo {
Abstract void method1 ();
Abstract void method2 ();
...
}

[Interface]
Do you know why you need interfaces with abstract classes? Because java is a single inheritance, but interfaces can inherit multiple!
Key Technologies for implementing most design patterns:
Interface Demo {
Void method1 ();
Void method2 ();
...
}

[Final class]

The final class cannot be inherited.
If you think that the definition of a class is perfect and you do not need to generate its subclass, you should also modify it as a final class.
Final class classname {...}

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.