/**
* 【练习题】1.多态练习
1.定义一个Animal父类,方法有eat();
2.定义三个子类;
Cat,Dog,Eagle;
每个子类增加新的方法,并重写eat();
3.利用多态性
定义一个Animal类型的变量a,并将不同子类的实例赋给a;
调用eat();观察结果后,并理解多态
4.思考,如果a的引用指向一个Cat,如何调用Cat的新增方法;
*/
package com.Oracle.oop4;
public class Animal {
public void eat() {
System.out.println("吃吃");
}
}
package com.Oracle.oop4;
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("咦,这是谁掉落的小鱼干(= =+)");
}
public void scorn() {
System.out.println("蔑视尔等凡人");
}
}
package com.Oracle.oop4;
public class Dog extends Animal {
public void eat() {
System.out.println("汪汪汪,我什么都吃哒~");
}
public void adhere() {
System.out.println("不粘人的狗不是好狗");
}
}
package com.Oracle.oop4;
public class Eagle extends Animal {
public void eat() {
System.out.println("作为一只鹰,吃肉不是理所当然的吗");
}
public void fly() {
System.out.println("想飞上天,和太阳肩并肩");
}
}
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();
}
}
}
/**
* 【练习题】2.多态练习
定义一个Animal类,方法有sing方法,定义这个类的三个子类(Dog,Cat,Bird),分别重写这个方法。利用多态,定义一个Animal类型的对象,
Animal a;分别引用三个子类的对象,调用sing方法。为每个子类,增加额外的方法。通过此例,练习upCast,downCast,及instanceof操作符。
*/
package com.Oracle.oop4;
public class Animal2 {
public void sing() {
System.out.println("啦啦,唱歌");
}
}
package com.Oracle.oop4;
public class Dog2 extends Animal2{
@Override
public void sing() {
System.out.println("我只会说:汪汪,会让你更旺哦");
}
public void adhere() {
System.out.println("不粘人的狗不是好狗");
}
}
package com.Oracle.oop4;
public class Cat2 extends Animal2 {
@Override
public void sing() {
System.out.println("喵喵,陪我玩啊");
}
public void scorn() {
System.out.println("蔑视尔等凡人");
}
}
package com.Oracle.oop4;
public class Bird2 extends Animal2{
@Override
public void sing() {
System.out.println("你去打听,十里八村数我小鸟唱歌最好听");
}
public void fly() {
System.out.println("想飞上天,和太阳肩并肩");
}
}
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();
}
}
}/**
* 【练习题】3.多态练习
模拟天天酷跑游戏;
定义一个(宠物)Pet类,类中有属性name,方法是follow(跟随);再定义三个子类,Cat,Dog,Eagle,分别重写follow方法;
再定义一个Hero类,这个类中有两个属性name和Pet类型的pet,一个方法run(),再定义两个构造方法,Hero(String name,);
Hero(String name,Pet pet);
run()方法的代码是Hero跑,并且他的宠物也跟着跑;
编写测试类来操作Hero
*/
package com.Oracle.oop4_2;
public class Pet {
String name;
public void follow() {
System.out.println(this.name+"一直跟随着您呦~");
}
}
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;
}
public void run() {
System.out.println("您的英雄 "+this.name+" 在跑,并且您的宠物 "+this.pet.name+"也跟着您的英雄跑");
}
}
package com.Oracle.oop4_2;
public class Cat extends Pet {
public void follow() {
System.out.println(this.name+"可以萌化凶狠的对手");
}
}
package com.Oracle.oop4_2;
public class Dog extends Pet{
public void follow() {
System.out.println(this.name+"保护您一路前行");
}
}
package com.Oracle.oop4_2;
public class Eagle extends Pet{
public void follow() {
System.out.println(this.name+"飞上了天,并和您肩并肩");
}
}
package com.Oracle.oop4_2;
public class Polymorphic3 {
public static void main(String[] args) {
Pet c=new Cat();
c.name="蓝胖子";
Hero h=new Hero("盖伦",c);
h.run();
}
}
/**
* 定义一个测试类Test,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
* 编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
*/
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;
}
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("该形状的面积为:"+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);
}
}
来源:CSDN
作者:陈衍舟
链接:https://blog.csdn.net/qq_39162424/article/details/78647319