先定一个宠物的抽象类,把所有共有的属性方法放到次类中,用于子类去继承实现。

package backing2;

abstract public class pet {
private String name;
private String sex;
private int age;
private int helthy;
private int happy;
private int hungry;
private boolean alive; public abstract void play();
public abstract void yiyuango();
public abstract void youeryuango();
public abstract void youlechanggo();
public abstract void eat(); public pet(String name, String sex) {
super();
this.name = name;
this.sex = sex;
this.age=1;
this.helthy=100;
this.happy=80;
this.hungry=70;
System.out.println("宠物名字:"+this.name+"\n"+" 性别:"+this.sex+"\t"+"年龄"+this.age+"健康值"+this.helthy+"饥饿值"+this.hungry);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHelthy() {
return helthy;
}
public void setHelthy(int helthy) {
this.helthy = helthy;
}
public int getHappy() {
return happy;
}
public void setHappy(int happy) {
this.happy = happy;
}
public int getHungry() {
return hungry;
}
public void setHungry(int hungry) {
this.hungry = hungry;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
}

  再分别定义宠物继承小类,子类实现重写父类的所有方法,注意:方法的权限修饰符不能小于父类的权限修饰符,在子类中再定义一个方法在每次使用每个方法的时候去判断一下宠物的几个属性值

package backing2;

public class qie extends pet {
public qie(String name, String sex) {
super(name, sex);
// TODO Auto-generated constructor stub
} @Override
public void play() {
System.out.println("小企鹅蹦蹦跳跳的玩去了");
this.setHappy(getHappy()+5);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void yiyuango() {
System.out.println("小企鹅蔫蔫的去医院了");
this.setHelthy(getHelthy()+20);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void youeryuango() {
System.out.println("小企鹅高高兴兴去上学");
this.setHungry(getHungry()+10);
this.setHappy(getHappy()+5);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void youlechanggo() {
System.out.println("小企鹅欢欢乐乐的去游乐场");
this.setHappy(getHappy()+10);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void eat() {
System.out.println("小企鹅吃鱼<。)#)))≦了");
this.setHungry(getHungry()-30);
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
}
public void check(){ if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){
System.out.println("您的宠物该去医院了") ;
}
this.getAge();
}
} package backing2; public class tuzi extends pet { public tuzi(String name, String sex) {
super(name, sex);
// TODO Auto-generated constructor stub
} @Override
public void play() {
check();
System.out.println("小兔子蹦蹦跳跳的玩去了");
this.setHappy(getHappy()+5);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void yiyuango() {
check();
System.out.println("小兔子蔫蔫的去医院了");
this.setHelthy(getHelthy()+20);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void youeryuango() {
check();
System.out.println("小兔子高高兴兴去上学");
this.setHungry(getHungry()+10);
this.setHappy(getHappy()+5);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void youlechanggo() {
check();
System.out.println("小兔子欢欢乐乐的去游乐场");
this.setHappy(getHappy()+10);
check();
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry());
} @Override
public void eat() {
check();
System.out.println("小兔子吃萝卜了");
this.setHungry(getHungry()-30);
System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); }
public void check(){ if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){
System.out.println("您的宠物该去医院了") ;
}
this.getAge();
}
}

 最后定义一个运行类,运行类里定义一个静态方法用来输出提示信息 

package backing2;

import java.util.Scanner;

public class testpet {

	public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("请输入您要选择的宠物");
System.out.println("1--兔子");
System.out.println("2--企鹅");
pet p=null;
String s1=s.nextLine();
if("1".equals(s1)){
System.out.print("请输入您的宠物名字");
String s2=s.nextLine();
System.out.print("请输入您的宠物性别");
String s3=s.nextLine();
p=new tuzi(s2,s3);
}else if("2".equals(s1)){
System.out.print("请输入您的宠物名字");
String s2=s.nextLine();
System.out.print("请输入您的宠物性别");
String s3=s.nextLine();
p=new tuzi(s2,s3);
}else{
System.out.println("其他宠物正在孵化中。。");
}
boolean falg=true;
while(falg){
testpet.tishi();
String s5=s.nextLine();
if("1".equals(s5)){
p.play();
}else if("2".equals(s5)){
p.yiyuango();
}else if("3".equals(s5)){
p.youlechanggo();
}else if("4".equals(s5)){
p.eat();
}else if("5".equals(s5)){
p.youeryuango();
}else if("exit".equals(s5)){
falg=false;
}else{
System.out.println("请按要求来好吗");
}
}s.close();
}
public static void tishi(){
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("exit--退出"); }
}

  

java宠物练习的更多相关文章

  1. java 宠物商店代码

    Pet.java interface Pet{ public String getName(); public String getColor(); public int getAge(); publ ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. Java实验项目三——宠物商店

    Program:宠物商店的设计(继承,接口,线性线性表) Description:本题未实现图形用户界面,项目结构描述如下: classes.Pet:定义宠物接口,只要实现该接口的宠物类,都可存储进宠 ...

  4. Java实例分析:宠物商店

    设计一个“宠物商店”,在宠物商店中可以有多种宠物,试表示出此种关系,并要求可以根据宠物的关键字查找相应的宠物信息. //======================================== ...

  5. Java实例---简单的宠物管理系统

    代码分析 Cat.java package com.ftl.petshop; class Cat implements Pet { private String name; private Strin ...

  6. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析

    interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; ...

  7. 使用Java实现数据库编程 项目(宠物商店)

    创建数据库代码: DROP DATABASE IF EXISTS petShop; CREATE DATABASE petShop; USE petShop; /*创建表*/ CREATE TABLE ...

  8. java基础知识总结(1)

    定义类: 访问修饰符 class 类名{ }   访问修饰符如:public .priate是可选的 class是声明类的关键字 按照命名规范,类名首字母大写   例:创建“人”类,关键代码: pub ...

  9. Java中PreparedStatement与Statement的总结

    概要: PreparedStatement 接口继承自 Statement 接口,PreparedStatement 比普通Statement 对象使用起来更加灵活,更有效率. 一.PreparedS ...

随机推荐

  1. 30多个Android 开发者工具 带你开发带你飞

    文中部分工具是收费的,但是绝大多数都是免费的. FlowUp 这是一个帮助你跟踪app整体性能的工具,深入分析关键的性能数据如FPS, 内存, CPU, 磁盘, 等等.FlowUp根据用户数量收费. ...

  2. css块级元素居中

    <!DOCTYPE html> <html> <head> <title>index</title> </head> <b ...

  3. opencv 删除二值化图像中面积较小的连通域

    对于上图的二值化图像,要去除左下角和右上角的噪点,方法:使用opencv去掉黑色面积较小的连通域. 代码 CvSeq* contour = NULL; double minarea = 100.0; ...

  4. 导入java项目时出现红色叹号问题的解决

    问题:导入java项目时出现红色叹号(如下图所示) 原因:引入项目的某些jar包跟自己电脑上的位置不一样: 解决方案:步骤如下 (1)右键红色叹号所在项目————>build path————& ...

  5. Visual Studio Code 使用心得

    Visual Studio Code 使用心得 最好用的跨平台编辑器,没有之一! 修改编辑器的显示语言 起因:vsCode又升级了(1.13.0),重启之后发现熟悉的中文菜单没有了,而且设置文件 se ...

  6. shared_ptr注意点

    1.原始指针不能直接使用赋值运算符对shared_ptr进行赋值运算 std::shared_ptr<int> p; p= ); //error p.reset(); //success ...

  7. 关于cas server无法通过session持久化方式实现集群的问题

    最近在搭建cas单点登录系统 ,在建立集群时发生一个问题. 搭建的环境是tomcat+tomcat-redis-session-manager+redis+cas 在对tomcat的session进行 ...

  8. 关于Dubbo分布式服务

    这篇文章写的详细,可参考 http://shiyanjun.cn/archives/1075.html

  9. [codeforces631E]Product Sum

    E. Product Sum time limit per test: 1 second memory limit per test: 256 megabytes input:standard inp ...

  10. 用html5调取谷歌地图获取位置

    function getmap(){ if(!navigator.geolocation) throw "Geolocation not supported"; var image ...