1.
/*
Multi-state exercise: Cat and dog case
*/
1 classAnimal {2 Public voideat () {3System.out.println ("Eat");4 }5 }6 7 classDogextendsAnimal {8 Public voideat () {9System.out.println ("Dog eats meat");Ten } One A Public voidLookdoor () { -System.out.println ("Dog janitor"); - } the } - - classCatextendsAnimal { - Public voideat () { +System.out.println ("Cat Eats fish"); - } + A Public voidPlayGame () { atSystem.out.println ("Cat Hide and Seek"); - } - } - - classDuotaitest { - Public Static voidMain (string[] args) { in //defined as a dog -Animal A =NewDog (); to a.eat (); +System.out.println ("--------------"); - //restore to a dog theDog d =(Dog) A; * d.eat (); $ D.lookdoor ();Panax NotoginsengSystem.out.println ("--------------"); - //Become a cat theA =NewCat (); + a.eat (); ASystem.out.println ("--------------"); the //restore to a cat +Cat C =(Cat) A; - c.eat (); $ c.playgame (); $System.out.println ("--------------"); - - //show the wrong content the //Dog dd = new Animal (); - //Dog ddd = new Cat ();Wuyi //classcastexception the //dog DD = (dog) A; - } Wu}
2, different local food culture of different cases
1 classPerson {2 Public voideat () {3System.out.println ("Eat");4 }5 }6 7 classSouthpersonextendsPerson {8 Public voideat () {9System.out.println ("Stir-fry, eat rice");Ten } One A Public voidJingshang () { -SYSTEM.OUT.PRINTLN ("Doing Business"); - } the } - - classNorthpersonextendsPerson { - Public voideat () { +System.out.println ("stewed vegetables, eat steamed buns"); - } + A Public voidYanjiu () { atSYSTEM.OUT.PRINTLN ("Research"); - } - } - - classDuoTaiTest2 { - Public Static voidMain (string[] args) { in //Test - //Southern People toPerson p =NewSouthperson (); + p.eat (); -System.out.println ("-------------"); theSouthperson SP =(Southperson) p; * sp.eat (); $ Sp.jingshang ();Panax NotoginsengSystem.out.println ("-------------"); - the //Northern People +p =NewNorthperson (); A p.eat (); theSystem.out.println ("-------------"); +Northperson NP =(Northperson) p; - np.eat (); $ Np.yanjiu (); $ } -}
Topic:
1, see the program to write the results: first to determine whether there is a problem, if not, write the results
1 classFu {2 Public voidShow () {3System.out.println ("Fu Show");4 }5 }6 7 classZiextendsFu {8 Public voidShow () {9System.out.println ("Zi Show");Ten } One A Public voidmethod () { -System.out.println ("Zi method")); - } the } - - classDuoTaiTest3 { - Public Static voidMain (string[] args) { +Fu f =NewZi (); - F.method (); + f.show (); A } at}
The answer is: Error, F.method () error here, parent class does not have this method
2, see the program to write the results: first to determine whether there is a problem, if not, write the results
1 classA {2 Public voidShow () {3 Show2 ();4 }5 Public voidShow2 () {6System.out.println ("I");7 }8 }9 classBextendsA {Ten Public voidShow2 () { OneSystem.out.println ("Love"); A } - } - classCextendsB { the Public voidShow () { - Super. Show (); - } - Public voidShow2 () { +System.out.println ("You"); - } + } A Public classDuoTaiTest4 { at Public Static voidMain (string[] args) { -A A =NewB (); - a.show (); - -b b =NewC (); - b.show (); in } -}
The answer is to love you.
public void Show () {
Show2 ();
The default is in front of Class B Show2
Features of polymorphic member access:
Method: Compile to see left, run to see right.
When inheriting:
Subclasses have the same method as in the parent class, called Overrides.
There are no methods in the subclass that the father has ever seen, and the method is inherited.
Java8-4 Exercises and topics