首页 > 分享 > 宠物类抽象与继承实践

宠物类抽象与继承实践

电子宠物示例

最新推荐文章于 2024-10-04 19:56:14 发布

weixin_30472035 于 2017-11-27 14:53:00 发布

  效果如下图:

public abstract class Pet {

private String name;

private int age;

private String sex;

private int cute;

private int clean;

private int sleep;

private boolean live;

public Pet() {

super();

}

public Pet(String name,String sex) {

super();

this.name = name;

this.age = 1;

this.sex = sex;

this.cute = 5;

this.clean = 50;

this.sleep = 50;

this.live = true;

}

public abstract void cutey ();

public abstract void cleany();

public abstract void sleepy() ;

public abstract void excuse();

public void show() {

System.out.println("名字"+this.name);

System.out.println("年龄"+this.age);

System.out.println("性别"+this.sex);

System.out.println("可爱度"+this.cute);

System.out.println("干净度"+this.clean);

System.out.println("睡眠度"+this.sleep);

}

public boolean check() {

if(this.clean<=20) {

System.out.println("该洗澡了");

}

if(this.sleep ==30) {

System.out.println("休息一下");

}else if(this.sleep ==10) {

live = false;

System.out.println("over");

}

return live;

}

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;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public int getCute() {

return cute;

}

public void setCute(int cute) {

this.cute = cute;

}

public int getClean() {

return clean;

}

public void setClean(int clean) {

this.clean = clean;

}

public int getSleep() {

return sleep;

}

public void setSleep(int sleep) {

this.sleep = sleep;

}

public boolean getLive() {

return live;

}

public void setLive(boolean live) {

this.live = live;

}

}

public class Dog extends Pet {

public Dog() {

super();

}

public Dog(String name, String sex) {

super(name, sex);

}

@Override

public void excuse() {

System.out.println("忠诚于您");

}

@Override

public void cutey() {

System.out.println(super.getName()+"摇尾巴 ,可爱度+5");

super.setCute(getCute()+5);

}

@Override

public void cleany() {

System.out.println(super.getName()+"在泥巴里玩,清洁度-10,");

super.setClean(getClean()-10);

}

@Override

public void sleepy() {

System.out.println(super.getName()+"趴在地上,睡眠度-10");

super.setSleep(getSleep()-10);

}

}

public class Cat extends Pet {

public Cat() {

}

public Cat(String name, String sex) {

super(name, sex);

}

@Override

public void excuse() {

System.out.println("是我的永远是我的");

}

@Override

public void cutey() {

System.out.println(super.getName()+"玩毛线 ,可爱度+5");

super.setCute(getCute()+5);

}

@Override

public void cleany() {

System.out.println(super.getName()+"爬树,清洁度-10");

super.setClean(getClean()-10);

}

@Override

public void sleepy() {

System.out.println(super.getName()+"睡在树上,睡眠度-10");

super.setSleep(getSleep()-10);

}

}

import java.util.Scanner;

import com.hanqi.test.model.Cat;

import com.hanqi.test.model.Dog;

import com.hanqi.test.model.Pet;

public class Test {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Pet p = null;

System.out.println("选择类型:");

System.out.println("1.泰迪 2.黑猫");

String s = scanner.nextLine();

System.out.println("起个名字吧");

String m =scanner.nextLine();

System.out.println("雄or雌");

String x = scanner.nextLine();

if("1".equals(s)) {

p = new Dog(m,x);

p.excuse();

}else {

p =new Cat(m,x);

p.excuse();

}

boolean f = true;

while(f) {

f = p.check();

System.out.println("请选择:0.显示,1.可爱模式 ,2.玩的地点,3.睡的地方 ,4.退出");

String w = scanner.nextLine();

if("0".equals(w)) {

p.show();

f = p.check();

} else if ("1".equals(w)) {

p.cutey();

f = p.check();

}else if ("2".equals(w)) {

p.cleany();

f = p.check();

}else if ("3".equals(w)) {

p.sleepy();

f = p.check();

}else if ("4".equals(w)) {

f = false;

}else {

System.out.println("请输入内容");

}

}

scanner.close();

}

}

转载于:https://www.cnblogs.com/zuo72/p/7898292.html

相关知识

Java猫和狗(继承,多态,抽象,接口版)上
宠物类与抽象方法实践
Java猫和狗(继承,多态,抽象,接口版)下
[Java] 继承与抽象类相关题目
java特性封装、抽象、继承、多态理论
Java编程 实现类的继承与多态 写一个动物类。成员变量为动物的种类,成员方法是动物叫声。
分别使用继承、多态、抽象类和接口方式实现动物类需求
【类、抽象与继承(练习)~python】
java对抽象类的理解与实践
编写Java程序,以继承和多态思想模拟饲养员喂养不同动物的不同行为

网址: 宠物类抽象与继承实践 https://m.mcbbbk.com/newsview931594.html

所属分类:萌宠日常
上一篇: 电子萌宠下载
下一篇: 电子宠物机加速器