Polymorphism brings up the problem of how to determine the type of object that a variable actually refers to. C + + uses Runtime-type information (RTTI), and Java uses instanceof operators.
The instanceof operator is used to determine the actual type of the object referenced by a variable, note the type of the object it refers to, not the type of the variable. Take a look at the following code:
1 Public Final classdemo{2 Public Static voidMain (string[] args) {3 //referencing an instance of the People class4People obj =Newpeople ();5 if(objinstanceofObject) {6System.out.println ("I am an Object");7 }8 if(objinstanceofpeople) {9System.out.println ("I am Human");Ten } One if(objinstanceofTeacher) { ASystem.out.println ("I am a teacher"); - } - if(objinstanceofPresident) { theSystem.out.println ("I Am the headmaster"); - } -System.out.println ("-----------");//Dividing Line - + //referencing an instance of the Teacher class -obj =NewTeacher (); + if(objinstanceofObject) { ASystem.out.println ("I am an Object"); at } - if(objinstanceofpeople) { -System.out.println ("I am Human"); - } - if(objinstanceofTeacher) { -System.out.println ("I am a teacher"); in } - if(objinstanceofPresident) { toSystem.out.println ("I Am the headmaster"); + } - } the } * classpeople{} $ classTeacherextendspeople{}Panax Notoginseng classPresidentextendsteacher{}
Operation Result:
I am an object
I am a human being
-----------
I am an object
I am a human being
I'm a teacher.
As you can see, if the variable refers to an instance of the current class or its subclasses, instanceof returns True, otherwise false is returned.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (27) the concept and implementation of inheritance
Java know how many super keywords
Java know how much (29) Overwrite and reload
Java know how much (30) polymorphic and dynamic binding
Java know how many static keywords and Java static variables and static methods
Java know how much (instanceof)
Java know how much (instanceof)