Java实验六:类的继承和多态
一、实验目的:
掌握类的继承和多态、抽象类及接口基本功能
二、实验内容:
1、设计一个图形类(Graph),设计其子类二维图形和三维图形,二维图形计算面积,三维图形计算体积;设计二维图形的子类圆形、矩形、三角形和梯形,分别计算其面积;设计三维图形的子类圆、长方体,计算其体积
package experiment_six;
import java.util.Scanner;
/*1、设计一个图形类(Graph),
* 设计其子类二维图形和三维图形,二维图形计算面积,三维图形计算体积;
* 设计二维图形的子类圆形、矩形、三角形和梯形,分别计算其面积;
* 设计三维图形的子类圆、长方体,计算其体积
2、模拟动物园饲养员给动物喂食。每一种动物需要吃不同的食物,饲养员针对不同的动物,喂不同的食物。
动物园里有狗(狗吃骨头,会做算术)、猫(猫吃鱼,会跳环)、老虎(老虎吃肉,会顶球)
试采用普通方式、抽象类方式、接口方式来进行多态的实现
*/
class Graph{
//static
double length;
//static
double width;
//static
double high;
//static
double r;
//static
double top_length;
//static
double bottom_length;
}
class TwoGraph extends Graph{
//int num;
//float area;
public /*static*/ void Show(){
System.out.println("============");
System.out.println("1.圆形\n2.矩形\n3.三角形\n4.梯形");
}
public /*static*/ void Choice(int num){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
if(num==1){
System.out.println("输入圆半径:");
r = input.nextDouble();
System.out.println("圆的面积:"+Circle.Circle_area(r));
System.out.println("============");
}
else if(num==2){
System.out.println("输入矩形长和宽:");
length = input.nextDouble();
width = input.nextDouble();
System.out.println("矩形的面积:"+JuXing.JuXing_area(length,width));
System.out.println("============");
}
else if(num==3) {
System.out.println("输入三角形底和高:");
length = input.nextDouble();
high = input.nextDouble();
System.out.println("三角形的面积:"+SanJiao.SanJiao_area(length,high));
System.out.println("============");
}
else if(num==4) {
System.out.println("输入矩形的上底、下底和高:");
top_length = input.nextDouble();
bottom_length = input.nextDouble();
high = input.nextDouble();
System.out.println("梯形的面积:"+TiXing.TiXing_area(top_length,bottom_length,high));
System.out.println("============");
}
else
System.out.println("ERROR");
}
}
class ThreeGraph extends Graph{
//float size;
public /*static*/ void Show(){
System.out.println("============");
System.out.println("1.球");
System.out.println("2.长方体");
}
public /*static*/ void Choice(int num) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
if(num==1){
System.out.println("输入球半径:");
r = input.nextDouble();
System.out.println("球的体积:"+Qiu.Qiu_size(r));
System.out.println("============");
}
else if(num==2) {
System.out.println("输入长方体长宽高:");
length = input.nextDouble();
width = input.nextDouble();
high = input.nextDouble();
System.out.println("长方体体积:"+FangTi.FangTi_size(length,width,high));
System.out.println("============");
}
else
System.out.println("ERROR");
}
}
class Circle extends TwoGraph{
public static double Circle_area(double r){
return (3.14*r*r);
}
}
class JuXing extends TwoGraph{
public static double JuXing_area(double length,double width){
return (length*width);
}
}
class SanJiao extends TwoGraph{
public static double SanJiao_area(double length,double high){
return (length*high/2);
}
}
class TiXing extends TwoGraph{
public static double TiXing_area(double top_length,double bottom_length,double high){
return ((top_length+bottom_length)*high/2);
}
}
class Qiu extends ThreeGraph{
public static double Qiu_size(double r){
return (4*3.14*r*r*r/3);
}
}
class FangTi extends ThreeGraph{
public static double FangTi_size(double length,double width,double high){
return (length*width*high);
}
}
class experiment_six_one{
public static void main(String[] args){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
flag:while(true) {
System.out.println("1.计算面积\n2.计算体积\n0.退出");
System.out.println("Choice the number:");
int num = input.nextInt();
switch(num){
case 1:TwoGraph twograph = new TwoGraph();
twograph.Show();
System.out.println("Choice the number:");
num = input.nextInt();
twograph.Choice(num);
break;
case 2:ThreeGraph threegraph = new ThreeGraph();
threegraph.Show();
System.out.println("Choice the number:");
num = input.nextInt();
threegraph.Choice(num);
break;
case 0:break flag;//带标签的break
}
}
}
}
2、模拟动物园饲养员给动物喂食。每一种动物需要吃不同的食物,饲养员针对不同的动物,喂不同的食物。
动物园里有狗(狗吃骨头,会做算术)、猫(猫吃鱼,会跳环)、老虎(老虎吃肉,会顶球)
试采用普通方式、抽象类方式、接口方式来进行多态的实现
package experiment_six;
interface Animal {
public void eat(Food food);
public void play();
}
/**
*@authorleno
*一种动物类:狗
*/
class Dog implements Animal {
public void eat(Food food) {
System.out.print("小狗吃"+food.getName());
}
public void play() {
System.out.println(",会做算术");
}
}
/**
*@authorleno
*一种动物类:猫
*/
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(",会顶球");
}
}
/**
*@authorleno
*食物抽象类
*/
abstract class Food {
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
*@authorleno
*一种食物类:骨头
*/
class Bone extends Food {
public Bone(String name) {
this.name = name;
}
}
/**
*@authorleno
*一种食物类:鱼
*/
class Fish extends Food {
public Fish(String name) {
this.name = name;
}
}
class Meat extends Food{
public Meat(String name) {
this.name = name;
}
}
/**
*@authorleno
*饲养员类
*
*/
class Feeder {
/**
*饲养员给某种动物喂某种食物
*@paramanimal
*@paramfood
*/
public void feed(Animal animal,Food food) {
animal.eat(food);
animal.play();
}
}
/**
*@authorleno
*测试饲养员给动物喂食物
*/
public class experiment_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);
}
}
1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验六:类的继承和多态
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验六:类的继承和多态