实验内容:
1、设计一个图形类(Graph),设计其子类二维图形和三维图形,二维图形计算面积,三维图形计算体积;设计二维图形的子类圆形、矩形、三角形和梯形,分别计算其面积;设计三维图形的子类圆、长方体,计算其体积
2、模拟动物园饲养员给动物喂食。每一种动物需要吃不同的食物,饲养员针对不同的动物,喂不同的食物。
动物园里有狗(狗吃骨头,会做算术)、猫(猫吃鱼,会跳环)、老虎(老虎吃肉,会顶球)
试采用普通方式、抽象类方式、接口方式来进行多态的实现
内容一代码:
//lsy
package java_experiments;
class Graph {
String name;
void showName() {
System.out.println("图形为:" + name);
}
void get() {
}
}
class TwoGraph extends Graph {
double area;
void get() {
System.out.println("图形的面积为:" + area);
}
}
class ThreeGraph extends Graph {
double volumn;
void get() {
System.out.println("图形的体积:" + volumn);
}
}
class _Circle extends TwoGraph {
double r;
_Circle(double r) {
name = "圆形";
area = 3.14 * r * r;
this.r = r;
}
}
class Rectangle extends TwoGraph {
double length;
double width;
Rectangle(double l, double w) {
name = "矩形";
area = l * w;
length = l;
width = w;
}
}
class Triangle extends TwoGraph {
double length;
double high;
Triangle(double l, double h) {
name = "三角形";
area = l * h;
length = l;
high = h;
}
}
class Trapezoid extends TwoGraph {
double upbottom;
double bottom;
double high;
Trapezoid(double ub, double b, double h) {
name = "梯形";
upbottom = ub;
bottom = b;
high = h;
area = (upbottom + bottom) * high / 2;
}
}
class Sphere extends ThreeGraph {
double r;
Sphere(double r) {
name = "球体";
volumn = (4 * 3.14 * r * r * r / 3);
this.r = r;
}
}
class Cuboid extends ThreeGraph {
double length;
double width;
double high;
Cuboid(double l, double w, double h) {
name = "长方体";
volumn = l * w * h;
length = l;
width = w;
high = h;
}
}
public class six_one {
public static void show(Graph graph) {
graph.showName();
graph.get();
}
public static void main(String[] args) {
Graph g;
g = new _Circle(2);// 圆形
show(g);
g = new Rectangle(3, 2);// 矩形
show(g);
g = new Triangle(2, 1);// 三角形
show(g);
g = new Trapezoid(2, 1, 1);// 梯形
show(g);
g = new Sphere(3);// 球体
show(g);
g = new Cuboid(3, 2, 1);// 长方体
show(g);
}
}
实验结果:
内容二代码:
//lsy
package java_experiments;
interface Animal {
public void eat(Food food);
public void play();
}
class Dog implements Animal {
public void eat(Food food) {
System.out.print("小狗吃" + food.getName());
}
public void play() {
System.out.println(",会做算术");
}
}
class Cat implements Animal {
public void eat(Food food) {
System.out.print("小猫吃" + food.getName());
}
public void play() {
System.out.println(",会跳环");
}
}
class Tiger implements Animal {
public void eat(Food food) {
System.out.print("老虎吃" + food.getName());
}
public void play() {
System.out.println(",会顶球");
}
}
abstract class Food {
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Bone extends Food {
public Bone() {
this.name = "骨头";
}
}
class Fish extends Food {
public Fish() {
this.name = "鱼";
}
}
class Meat extends Food {
public Meat() {
this.name = "肉";
}
}
class Feeder {
public void feed(Animal animal, Food food) {
animal.eat(food);
animal.play();
}
}
public class six_two {
public static void main(String[] args) {
Feeder feeder = new Feeder();
Animal animal = new Dog();
Food food = new Bone();
feeder.feed(animal, food); // 给狗喂骨头
animal = new Cat();
food = new Fish();
feeder.feed(animal, food); // 给猫喂鱼
animal = new Tiger();
food = new Meat();
feeder.feed(animal, food); // 给老虎喂肉
}
}