/** * "Exercises" 1. Polymorphism Exercise 1. Define a animal parent class with Eat ();
2. Define three subclasses; cat,dog,eagle;
Each subclass adds a new method and overrides the Eat (); 3. Use polymorphism to define a animal type of variable A, and assign instances of different subclasses to A; call eat (); Observe the results and understand polymorphism 4. Think, if a reference points to a cat, how to call the new method of cat; */package com.
ORACLE.OOP4;
public class Animal {public void Eat () {System.out.println ("eat"); } package com.
ORACLE.OOP4;
The public class Cat extends animal{@Override the public void Eat () {System.out.println ("Gee, this is the little fish (= =+) that fell)";
public void Scorn () {System.out.println ("contempt for mortals"); } package com.
ORACLE.OOP4;
public class Dog extends Animal {public void Eat () {System.out.println ("I eat everything ~");
public void Adhere () {System.out.println ("non-sticky dog is not a good dog"); } package com.
ORACLE.OOP4;
public class Eagle extends Animal {public void Eat () {System.out.println ("as an eagle, eating meat is not taken for granted");
public void Fly () {System.out.println ("Want to Fly to heaven, shoulder by side"); } package com.
ORACLE.OOP4; public class Polymorphic1 {public static void main (string[] args) {Animal A=new Cat ();
A.eat ();
A=new Dog ();
A.eat ();
A=new Eagle ();
A.eat ();
if (a instanceof cat) {cat c= (cat) A;
C.scorn ();
}else if (a instanceof Dog) {Dog d= (Dog) A;
D.adhere ();
}else if (a instanceof Eagle) {Eagle e= (eagle) A;
E.fly ();
}
}
}
/** * "Exercises" 2. The polymorphic exercise defines a animal class, which has a sing method that defines the three subclasses (Dog,cat,bird) of the class and overrides this method respectively. Using polymorphism, define an object of type Animal, Animal A; Refer to objects of three subclasses, and invoke the Sing method. For each subclass, add additional methods.
In this case, practice upcast,downcast, and the instanceof operator. * * Package com.
ORACLE.OOP4;
public class Animal2 {public void sing () {System.out.println ("La La, Sing"); } package com.
ORACLE.OOP4;
The public class Dog2 extends animal2{is @Override public void Sing () {System.out.println ("I will only say:" Wang Woo, will make you more prosperous ");
public void Adhere () {System.out.println ("non-sticky dog is not a good dog"); } package com.
ORACLE.OOP4;
public class Cat2 extends Animal2 {@Override public void sing () {System.out.println ("Meow, play with Me");
public void Scorn () {System.out.println ("contempt for mortals"); } package com.
ORACLE.OOP4;
The public class Bird2 extends animal2{@Override the public void Sing () {System.out.println ("You inquire, ten miles eight villages count my birds to sing best");
public void Fly () {System.out.println ("Want to Fly to heaven, shoulder by side"); } package com.
ORACLE.OOP4; public class Polymorphic2 {public static void main (string[] args) {ANimal2 a=new Cat2 ();
A.sing ();
A=new Dog2 ();
A.sing ();
A=new Bird2 ();
A.sing ();
if (a instanceof Cat2) {Cat2 c= (CAT2) A;
C.scorn ();
}else if (a instanceof Dog2) {Dog2 d= (Dog2) A;
D.adhere ();
}else if (a instanceof Bird2) {Bird2 b= (Bird2) A;
B.fly (); }
}
}
/** * "Exercise" 3. Multi-State practice simulates the cool running game every day; define a pet class that has attribute name in the class, follow (following), and then define three subclasses, Cat,dog,eagle, rewrite follow method respectively, and then define a hero class,
This class has two property name and pet type pet, one method run (), and then define two construction methods, Hero (String name,);
Hero (String name,pet Pet); The code for the Run () method is hero run, and his pet runs along with it, writing test classes to manipulate Hero/package com.
Oracle.oop4_2;
public class Pet {String name;
public void Follow () {System.out.println (this.name+ "always follows you ~"); } package com.
Oracle.oop4_2;
public class Hero {String name;
Pet Pet;
Public Hero (String name) {this.name=name;
Public Hero (String name, pet pet) {this.name = name;
This.pet = pet;
The public void Run () {System.out.println ("Your Hero" +this.name+ "is running, and your pet" +this.pet.name+ "follows your hero"); } package com.
Oracle.oop4_2;
public class Cat extends Pet {public void follow () {System.out.println (this.name+ "Can erupt vicious opponents"); } package com.
Oracle.oop4_2;
The public class Dog extends pet{the public void Follow () {System.out.println (this.name+ "protect you all the way forward"); } package com.
Oracle.oop4_2; Public Class Eagle extends pet{public void follow () {System.out.println (this.name+ "flew up and down with You"); } package com.
Oracle.oop4_2;
public class Polymorphic3 {public static void main (string[] args) {Pet c=new Cat ();
C.name= "Blue fat";
Hero h=new Hero ("Galen", c);
H.run ();
}
}
/** * Define a test class testing, write the Equalsarea method to test the area of two objects is equal (note the parameter type of the method, using dynamic binding technology), * Write the Displaygeometricobject method to display the area of the object (note the parameter type of the method,
Leveraging dynamic binding techniques). * * Package com.
Oracle.oop4_2;
public class Geometricobject {static final double pi=3.1415926;
String color;
Double weight;
Public String GetColor () {return color;
public void SetColor (String color) {this.color = color;
Public double Getweight () {return weight;
The public void Setweight (double weight) {this.weight = weight;
Public Geometricobject (String color, double weight) {this.color = color;
This.weight = weight;
Public double Findarea () {double a=0;
return A;
public boolean Equalsarea (Geometricobject c,geometricobject m) {if (C.findarea () ==m.findarea ()) {return true;
return false;
public void Displaygeometricobject (Geometricobject g) {System.out.println ("the area of the shape is:" +g.findarea ()); } package com.
Oracle.oop4_2;
public class MyRectangle extends Geometricobject {double width; Double HeighT
Public double getwidth () {return width;
public void SetWidth (double width) {this.width = width;
Public double getheight () {return height;
public void SetHeight (double height) {this.height = height;
Public MyRectangle (double width,double height,string color,double weight) {super (color,weight);
This.width=width;
This.height=height;
@Override public Double Findarea () {double s=width*height;
return s; } package com.
Oracle.oop4_2;
public class Circle extends geometricobject{//static final double pi=3.1415926;
Double radius;
Public double Getradius () {return radius;
public void Setradius (double radius) {This.radius = radius;
Public Circle (double radius,string color,double weight) {super (color,weight);
This.radius=radius;
Public double Findarea () {double S=geometricobject.pi*radius*radius;
return s; } package com.
Oracle.oop4_2; public class Polymorphic4 {public static void main (string[] args) {GeoMetricobject g1=new Circle (5.2, "blue", 4.3);
Geometricobject g2=new MyRectangle (8.7,9.6, "yellow", 5.1);
System.out.println (G1.equalsarea (G1, G2));
G1.displaygeometricobject (G1);
G2.displaygeometricobject (G2);
}
}