Reference blog: Dot Me
Important: The normal function in Java defaults to a virtual function, so the behavior of dynamic binding is the default, and C + + must declare a method as virtual (virtual keyword), which is dynamically bound when executed, with a detailed distinction between code and annotations.
The code roughly: implements the parent class animal class, contains the data member name and the age, as well as implements the Eat method and the Informa method, the subclass dog class inherits animal, and implements the method the overwrite. Both Java and C + + are not shown as virtual functions, but the observation output shows that Java implements dynamic binding, and C + + does not, only the corresponding function with the virtual keyword, the implementation of dynamic binding. This is the difference between Java and C + + processing
C + + code:
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;classanimal{ Public: Charname[ -]; intAge ; Animal () { age=0; name[0] =0; } Animal (Char* Nname,CharNAge) {strcpy (name, nname); Age=NAge; } voideat () {printf ("Animal can eat!\n"); } //Here you can add the virtual keyword to observe the output difference voidinformation () {printf ("%s is a Animal, it ' s%d years old!\n", name, age); }};classDog: Publicanimal{ Public: Dog (): Animal () {} dog (Char* Nname,intnAge): Animal (Nname, NAge) {}voideat () {printf ("Dog can eat!\n"); } voidinformation () {printf ("%s is a Dog, it ' s%d years old!\n", name, age); }};intMain () {Animal* A =NewAnimal ("A",Ten); Animal* B =NewDog ("B", -); A-information (); //C + + must show that it is declared as a virtual function to be polymorphic, or else it simply calls the method of the parent class and does not call the child class.B->information (); return 0;}View Code
Java code:
Animal class:
PackageCom.hao; Public classAnimal {String name= ""; intAge = 0; Animal () {}/** * @paramName of the animal *@paramage of an animal*/ PublicAnimal (String name,intAge ) { Super();//its super class is actually the object class This. Name =name; This. Age =Age ; } Public voideat () {System.out.println ("Animal can eat!"); } Public voidinformation () {System.out.printf ("%s is a Animal, it ' s%d years old!\n", name, age); } }//AnimalView Code
Dog class:
PackageCom.hao; Public classDogextendsAnimal {/*** Default constructor*/ PublicDog () {Super(); //TODO Auto-generated constructor stub } /*** @function with parameter constructor *@paramName of dog name *@paramage of the dog*/ PublicDog (String name,intAge ) { Super(name, age); //TODO Auto-generated constructor stub } /*(non-Javadoc) * @see com.hao.animal#eat ()*/@Override Public voideat () {//TODO Auto-generated method stubs//super.eat ();System.out.println ("Dog can eat!"); } /*(non-Javadoc) * @see com.hao.animal#information ()*/@Override Public voidinformation () {//TODO Auto-generated method stubs//super.information ();System.out.printf ("%s is a Dog, it ' s%d years old!\n", name, age); } }View Code
The main class that contains the primary function:
package Com.hao; public class Main { static void main (string[] args) {// TODO auto-generated method stub Animal A = new Animal ("A", 10 = new Dog ("B", 20 // java uses dynamic bindings by default---differs from C + + A.eat (); B.eat (); }}
View Code
The similarities and differences of virtual functions between Java and C + +