The object class is the parent class for all classes:
Our upper and lower code:
1 packagecom.learn.chap03.sec14;2 3 public classA//the above Class A inherits the Object class, so it can be defined as: public class A extends object{4 /**5 * The object class is the parent class of all classes6 */7 publicA () {8 Super();9 }Ten}
We can accomplish our own logic by polymorphic, using the ToString () and equals (object obj) methods that are commonly used to rewrite the object class .
1 packagecom.learn.chap03.sec14;2 3 public classpeople {4 PrivateString name;5 6 /**7 * Construction Method8 * @paramname9 */Ten publicpeople (String Name) { one this. Name =name; a } - - publicString getName () { the returnname; - } - - public voidsetName (String Name) { + this. Name =name; - } + a /** at * Overridden The ToString () method of the object class - */ - @Override - publicString toString () { - return this. GetName (); - } in - /** to * overridden the Equals (object Obj) method of the object class + */ - @Override the public Booleanequals (Object obj) { * String name = ((people) obj). getName (); $ return this.name = = name;Panax Notoginseng } - the public Static voidmain (string[] Args) { +People p1 =NewPeople ("zhangsan"); aPeople p2 =NewPeople ("zhangsan"); thePeople p3 =NewPeople ("lisi"); + //p1.equals (p2); -System.out.println ("p1.equals (p2):" + p1.equals (p2)); $System.out.println ("p1.equals (p3):" + p1.equals (p3)); $ System.out.println (p1); - System.out.println (p1.tostring ()); - System.out.println (p2); the System.out.println (p2.tostring ()); - System.out.println (p3);Wuyi System.out.println (p3.tostring ()); the - } wu}
Operation Result:
P1.equals (p2): True
P1.equals (p3): False
Zhangsan
Zhangsan
Zhangsan
Zhangsan
Lisi
Lisi
instanceof Keywords
Function: determine whether an object belongs to a class
Format: object instanceof Class (return Boolean Type)
A downward transformation to make judgments
Our upper and lower code:
1 package com.learn.chap03.sec15; 2 Public class Animal { 4 public void say () { 6 SYSTEM.OUT.PRINTLN ("i am an Animal!") " 7 8 }
package com.learn.chap03.sec15; public class Dog extends animal{@Override public void say () {System.out.println ( "i am a dog" ); /** * Define Subclass method */ public void F1 () {System.out.println ( "dog likes chew bones"
package com.learn.chap03.sec15; public class extends Animal { @Override publicvoid say () { System.out.println (" I am a cat "); /** * Define Subclass Method * /public void F2 () { System.out.println ("cat likes to eat fish");} }
packagecom.learn.chap03.sec15; public classTest {/*** static method cannot invoke Non-static method The following main method is static, so the DoSomething method must be static method *@paramAnimal*/ public Static voiddosomething (Animal Animal) {animal.say (); if(animalinstanceofDog) {//down transformation to make a judgment, so the following code will be safe(Dog) animal). F1 (); } if(animalinstanceofCat) {//down transformation to make a judgment, so the following code will be safe((Cat) animal). F2 (); } } public Static voidmain (string[] Args) {Animal dog=NewDog (); System.out.println ("does the Dog object belong to the animal class:" + (doginstanceofAnimal)); System.out.println ("does the Dog object belong to the dog class:" + (doginstanceofDog)); System.out.println ("does the Dog object belong to cat class:" + (doginstanceofCat)); DoSomething (NewDog ()); DoSomething (NewCat ()); }}
Operation Result:
Whether the dog object belongs to the animal class: true
Whether the dog object belongs to the dog class: true
Whether the dog object belongs to Cat class: false
I'm A Dog.
Dogs like Chew Bones.
I'm A Cat.
Cats like to eat fish
Java object class and instanceof keyword