java实验六 类的继承和多态

该博客介绍了使用Java实现图形类的继承与多态,包括二维图形(圆形、矩形、三角形、梯形)和三维图形(球体、长方体)的面积和体积计算。同时,通过接口方式实现了动物园喂食场景的多态,展示了狗、猫和老虎吃不同食物并展示相应技能的行为。
摘要由CSDN通过智能技术生成

实验内容:

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); // 给老虎喂肉
	}
}

实验结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值