Example to see polymorphism

zhaozj2021-02-16  51

Rel = file-list href = "instance looks polymorphism .files / filelist.xml">

Example to see inheritance and polymorphism

Example to see polymorphism

I have seen a polymorphism and record some of my feelings in the form of example.

One. The image understands the principles of understanding: (1) A derived class object can be declared into a base class, or a base class pointer can point to a derived class object: // C code

Baseclass * p;

DerivedClass Obj;

P = & obj;

// c # code

Baseclass obj = new derivedclass ();

(2) Take an object as a separate individual, call the object's public member function actually sends a message to this object, what kind of action is fully determined by the object.

Shape is a base class, Circle and Line are inherited from Shape, and Shape has a Draw () method, Circle and Line define their own Draw () method, in the following code:

// java code

Static Void Func (Shape S)

{

s.draw ();

}

If this happens:

Line L = new line ();

Circle C = New circle ();

FUNC (L);

Func (c);

A Circle and a Line are sent to the function when making shape, then call Draw (), what happens? Because the object is a separate individual, in func (), these two objects are transmitted separately, and they call themselves, so they call the DRAW () action defined in their own class.

Through these two principles we can understand the above polymorphism. It is because of the polymorphism, so that we don't have to do this:

IF You are a circle kilocuses calling circle Draw ()

Else If You are a Line Then called Line Draw ()

Else ...

Just send a Draw message to this statement that is declared as a Shape, how to go to Draw is determined by the object.

two. Everything is virtual function

Let's take a look at the basic conditions of achieving polymorphism:

(1)

Base class contains virtual functions

(2)

Inheritance class reinforce this virtual function

(3)

Inheritance classes may not re-realize all virtual functions of the base class, so it is not possible for these virtual functions that are not re-implemented.

Look at some special provisions in several languages:

1.

C : (1) The virtual function is declared with the Virtual keyword. (2) Virtual void func (para_list) = 0; this virtual function is called a pure virtual function, indicating that this function is not specific. The class containing the pure virtual function is called an abstract class. If his inheritance class does not implement this pure virtual function, this inheritance class is also an abstract class. Abstract classes cannot be instantiated (that is, an object cannot be created). (3) When the inheritance is re-realizing the virtual function of the base class, it is not necessary to do any special statement. (4) If you do not need to modify the virtual keyword, and re-implement this method in the derived class, this is just a simple coverage, which will not happen, we are temporarily called it non-polyure.

2.

Java: (1) Java has no virtual keyword, Java's method of treating all classes is a virtual function. (2) When inheriting the virtual function of the base class, it is not necessary to do any special statement. Therefore, in Java, as long as the base class is reached, and the inheritance object is declared as a base class, the polymorphism will happen. Therefore, Java is relatively low for polymorphism. // Java Code class BaseClass {public void hello () {};} class DerivedClass extends BaseClass {public void hello () {System.out.println ( "Hello world!");} Public static void main (String args [] ) {Baseclass obj = new derivedclass (); obj.hello ();}} input is Hello World !. This achieves a polymorphism. (3) The virtual function is declared with the Abstract, and the class containing the virtual function is an abstract class, but also uses the Abstract keyword. // java code public abstract Abstractclass {public abstract void hello (); // ...} 3.

C #: C # is the most stringent and rigorous writing. (1) The virtual function is declared by Virtual. (2) Pure virtual functions are declared with Abstract, and the class containing pure virtual functions is an abstract class that must be modified with Abstract keywords. (3) If only the non-virtual method of the base class, you need to declare the new keyword: // c # code public class baseclass {public void hello () {system.console.writeline ("Hello, this com" );}} public class DerivedClass: BaseClass {public new void hello () {System.Console.WriteLine ( "Hello, this is come from DerivedClass");} public static void Main () {BaseClass obj = new DerivedClass (); Obj.hello ();}} Output is Hello, this come from baseclass, that is, this does not realize polymorphism (non-polymorphism). (4) A polymorphism is achieved by Virtual - Override, ABSTRACT - OVERRIDE. When derived, you must re-implement the virtual function (or pure virtual function) of the base class, you must modify it with the Override keyword. // C # Code public abstract class AbsBaseClass {public abstract void hello ();} public class DerivedClass: AbsBaseClass {public void hello () {System.Console.WriteLine ( "! Hello world");} public static void SayHello (AbsBaseClass obj ) {Obj.hello ();} public static void main () {derivedclass _obj = new derivedclass (); derivedclass.sayhello (_obj);}} Output is Hello World! When a polymorphic across transistor is in a polymorphism, it does not completely throw the base class regardless of whether it will look at the virtual function list of the base class, and the polymorphism will occur within the scope of this list. Let us look at a more complicated example: // java code class a

{

Protected void Hello (Object O)

{

System.out.println ("A - Object");

}

}

Class B Extends a

{

Protected void Hello (String S)

{

System.out.println ("B - String");

}

Protected void Hello (Object O)

{

System.out.println ("B - Object");

}

}

Class C

{

Public static void main (string args [])

{

Object obj = new object ();

String str = "abc";

A a = new b ();

A.hello (OBJ); A. Hello (STR);

}

}

The output is:

B - Object

B - Object

As mentioned above, because there is no parameter type in the base class, the B's Hello (String) method does not participate in the polymorphism. When calling A.Hello (STR), since String is an inheritance class of Object, this STR is incorporated into b's Hello (Object) as an Object, as of our principles.

four. Interface - just more abstract abstract classes

The interface is an agreement of the class, but because the interface is involved in polymorphism, from this point, we think it is a more abstract abstraction class, as follows:

// java code interface ibase

{Void Hello ();} class derivedclass imports ibase {public void Hello () {system.out.println ("Hello World!");} Public static void main (string args []) {ibase obj = new derivedclass () Obj.hello ();}}

In Java and C #, classes can only be derived from one base class, but multiple interfaces can be implemented.

Here is a small problem: if ibase1 and ibase2 declare that there is a hello () method, DeriveDClass implements these two interfaces, of course, it is necessary to implement Hello ().

interface IBase1 {void hello ();} interface IBase2 {void hello ();} public class DerivedClass1: IBase1, IBase2 {public void hello () {System.Console.WriteLine ( "! Hello world");}} public class DerivedClass2 : Ibase1, ibase2 {void ibase1.hello ()

{System.Console.writeline ("this come from ibase

1"

); Void ibase2.hello () {system.console.writeline ("this come from ibase

2"

}

public static void Main () {IBase1 obj_1 = new DerivedClass1 (); IBase2 obj_2 = new DerivedClass1 (); IBase1 obj_3 = new DerivedClass2 (); IBase2 obj_4 = new DerivedClass2 (); obj_1.hello (); obj_2.hello () Obj_3.hello (); obj_4.hello ();}}

The output is:

Hello World! Hello World !;

This come from ibase1

This come from ibase2

There are two points: (1) DeriveDClass2 implementation method is explicitly realized, this method C # is supported, and cannot be implemented in Java. (2) Further test shows that the Hello () method is not derivedclass2:

Join this code

DerivedClass2 T = new derivedclass2 ();

t.hello ();

Compiling error: Test.cs (44, 3): Error CS0117: "DeriveDClass2" does not include the definition of "Hello"

That is to say, this method is an interface, but the interface cannot contain specific implementation code, is there a certain contradiction here?

Welcome to communicate with me: tanrui@sjtu.edu.cn

转载请注明原文地址:https://www.9cbs.com/read-23062.html

New Post(0)