Java面向对象之——马戏团节目管理程序

成功在于点滴的积累,少年加油,今天的付出就是明天的收获!

多敲代码,多写小项目是巩固自己知识点最好的方式之一,同时也是大脑的思维能力得到提升的有效途径。

流程分析

整个项目由三个环节组成

  1. 表演菜单展示

  2. 选择表演者进行表演
    ——每个表演者的表演信息是通过调用act()方法输出的

  3. 选择是否继续观看表演

详细设计

抽象父类

/**
 * 抽象父类Animal
 */
public abstract class Animal {
	
	private String name;//昵称
	
	private int age;//年龄
	
	public abstract void love();//抽象方法——描述喜好
	//无参构造方法
	public Animal() {
		super();
	}
	//有参构造方法
	public Animal(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

接口

/**
 * 接口IAct
 */
public interface IAct {
	
	void skill();//抽象方法——描述技能
	
	void act();//抽象方式——描述表演
}

实现类

棕熊(Bear)

/**
 * 继承自Animal类的Bear类
 * 接口IAct
 */
public class Bear extends Animal implements IAct {

    //无参构造方法 
	public Bear() {
		super();
	}
	//有参构造
	public Bear(String name,int age){
		super(name,age);
	}
	@Override
	public void skill() {//重写技能方法
		System.out.println("技能:挽着花篮,打着雨伞,自立走秀");
	}
	@Override
	public void act() {//重写表演方法
		System.out.println("表演者:"+this.getName());
		System.out.println("年龄:"+this.getAge()+"岁");
		skill();
		love();
	}
	@Override
	public void love() {//重写喜好方法
		System.out.println("爱好:喜欢卖萌");
	}
}

狮子(Lion)

/**
 * 继承自Animal类的Lion类
 * 接口IAct
 */
public class Lion extends Animal implements IAct {
	
	private String color;//颜色
	
	private String sex;//性别
	
	//无参构造
	public Lion() {
		super();
	}
	//有参构造
	public Lion(String color, String sex) {
		super();
		this.color = color;
		this.sex = sex;
	}
	public Lion(String name,int age,String color, String sex){
		super(name,age);
		this.color = color;
		this.sex = sex;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public void skill() {//重写技能方法
		System.out.println("技能:擅长钻火圈");
	}
	@Override
	public void act() {//表演方法
		System.out.println("表演者:"+this.getName());
		System.out.println("年龄:"+this.getAge()+"岁");
		System.out.println("性别:"+this.getSex());
		System.out.println("颜色:"+this.getColor());
		skill();
		love();
	}
	@Override
	public void love() {//重写喜好方法
		System.out.println("爱好:喜欢吃各种肉类");
	}
}

猴子(Monkey)

/**
 * 继承自Animal类的Monkey类
 * 接口IAct
 */
public class Monkey extends Animal implements IAct {
	
	private String type;//品种
	
	//无参构造
	public Monkey() {
		super();
	}
	//有参构造
	public Monkey(String name,int age,String type) {
		super(name,age);
		this.type = type;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@Override
	public void skill() {//重写技能方法
		System.out.println("技能:骑独轮车过独木桥");
	}
	@Override
	public void act() {//表演方法
		System.out.println("表演者:"+this.getName());
		System.out.println("年龄:"+this.getAge()+"岁");
		System.out.println("品种:"+this.getType());
		skill();
		love();
	}
	@Override
	public void love() {//重写喜好方法
		System.out.println("爱好:喜欢模仿人的动作表情");
	}
}

鹦鹉(Parrot)

/**
 * 继承自Animal类的Parrot类
 * 接口IAct
 */
public class Parrot extends Animal implements IAct {
	
	private String type;//品种
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;//品种
	}
	//无参构造
	public Parrot() {
		super();
	}
	//有参构造
	public Parrot(String name,int age,String type) {
		super(name,age);
		this.type = type;
	}

	@Override
	public void skill() {//重写技能方法
		System.out.println("技能:擅长模仿");
	}
	@Override
	public void act() {//重写表演方法
		System.out.println("表演者:"+this.getName());
		System.out.println("年龄:"+this.getAge()+"岁");
		System.out.println("品种:"+this.getType());
		skill();
		love();
	}
	@Override
	public void love() {//重写喜好方法
		System.out.println("爱好:喜欢吃坚果和松子");
	}
}

小丑(Clown)

/**
 * 实现类Clown
 * 接口IAct
 */
public class Clown implements IAct {
	private String name;//名字
	private int years;//艺龄
	//无参构造
	public Clown() {
		super();
	}
    //有参构造
	public Clown(String name, int years) {
		super();
		this.name = name;
		this.years = years;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getYears() {
		return years;
	}
	public void setYears(int years) {
		this.years = years;
	}
	@Override
	public void skill() {//重写技能方法
		System.out.println("技能:脚踩高跷,进行杂技魔术表演");
	}
	@Override
	public void act() {//重写表演方法
		System.out.println("表演者:"+this.getName());
		System.out.println("年龄:"+this.getYears()+"年");
		skill();
		dress();
	}
	public void dress(){//着装特点独有方法
		System.out.println("着装:身穿五彩服装,头上戴着彩色的帖子,脸上画着夸张的彩妆");
	}
}

测试(Test)

import java.util.Scanner;
/**
 * 测试类
 */
public class Test {
	/**
	 * 第一部分:表演菜单展示
	 * 第二部分:选择表演者进行表演
	 * 第三部分:选择是否继续观看表演
	 *提示信息 
	 */
	//第一部分:表演菜单展示
	public void notice(){
		System.out.println("*********欢迎来到太阳马戏团*********");
		System.out.println("*********   请选择表演者     *********");
		System.out.println("*********      1、棕熊        *********");
		System.out.println("*********      2、狮子        *********");
		System.out.println("*********      3、猴子        *********");
		System.out.println("*********      4、鹦鹉        *********");
		System.out.println("*********      5、小丑        *********");
		System.out.println("*********      0——退出        *********");
		System.out.println("***********************************");
	}

	public static void main(String[] args) {
		Test test =new Test();
		Scanner sc=new Scanner(System.in);
		int input=0;//用于接收选择表演者
		int input1;//用于接收是否继续观看
		IAct actor=null;//接口引用指向实现类实例
		while(true){
			test.notice();//调用第一部分
			System.out.println("请输入对应的数字观看表演:");
			try{
				input = sc.nextInt();	
				}catch(Exception e){
					System.out.println("输入有误,请按照提示的整数输入");
					sc.next();
					continue;
				}
			if(input==0){
				System.out.println("退出程序!");
				break;
			}
			switch(input){//第二部分根据选择表演者,实现表演者的节目展示
			case 1:
				actor=new Bear("Bill",1);//接口引用指向实现类实例--多态
				actor.act();//调用表演方法
				break;
			case 2:
				actor=new Lion("Lian",2,"公狮","灰色");
				actor.act();
				break;
			case 3:
				actor=new Monkey("Tom",1,"金丝猴");
				actor.act();
				break;
			case 4:
				actor=new Parrot("Rose",1,"牡丹鹦鹉");
				actor.act();
				break;
			case 5:
				actor=new Clown("Kahle",5);
				actor.act();
				break;
			default:
			    System.out.println("输入有误,请重新输入");
			      continue;	
			}
			//第三部分:选择是否继续观看
			Boolean b=true;
			while(b) {
				System.out.println("******" + "是否继续观看(1/0)" + "******");
				try {
					input1 = sc.nextInt();
				} catch (Exception e) {
					System.out.println("输入有误,请按照提示的整数输入");
					sc.next();
					continue;
				}
				switch (input1) {
				case 1:
					b = false;
					break;
				case 0:
					System.out.println("*********" + "欢迎下次光临" + "*********");
					System.exit(0);
					break;
				default:
					System.out.println("输入信息不正确,请重新输入");
					continue;
				}
			}
		}
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值