一:
public class Pet {
private String name;
private int health;
private int love;
PetShop(String name,int health,int love){
this.name=name;
this.health=health;
this.love=love;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getHealth(){
return health;
} public void setHealth(int health){ if(health>100||health<0) { System.out.println("健康值应在0到100之间,默认值是40");} else this.health=health; } public int getLove(){ return love; } public void setLove(int love){ this.love=love; } public void speak(){ System.out.println("Hi,主人,我的名字是"+name+"我的健康值是"+health+"我和主人的亲密度是" +love); }
public void Petshop
public static void main(Sting[]args)
{Scanner input =new Scanner(System.in);
System.outprintln("欢迎来到宠物店");
System.out.println("请输入要领养宠物的名字:");
String name=input.next();
System.out.prinln("请输入要领养的宠物类型:(1.狗狗2.企鹅)");
swith(input.nextInt())
{ case 1:
System.out.println(“请选择狗狗的品种:(1.聪明的拉布拉多犬” + “ 2.酷酷的雪纳瑞)”);
String strain = null;
if(input.nextInt() == 1){
strain = ”聪明的拉布拉多犬”;
} else {
strain = “酷酷的雪纳瑞”;
}
Dog dog = new Dog();
dog.name = name;
dog.strain = strain;
dog.print();
break;
case 2:
System.out.println(“请选择企鹅的性别:(1.Q仔 2.Q妹)”);
String sex = null;
if(input.nextInt() == 1)
sex = ” Q仔”;
else
sex = “Q妹”
Penguin pgn = new Penguin();
pgn.name = name;
pgn.sex = sex;
pgn.print();
}
}
}
class Dog extends Pet{private String breed;
public Dog(String name,int health,int love,Strint breed)
{super(name,health,love);
this.breed=breed;}
public String getBreed(){
return breed;
}
public void setBreed(String breed){
this.breed=breed;
}
public void speak(){
super.speak();
System.out.println("我的品种是"+breed);
}
}
class Penguin extends pet
{private String sex;
public Penguin(String name,int health,int love,Strint breed)
{super(name,health,love);
this.sex=sex;}
public String getsex(){
return sex;
}
public void setsex(String sex){
this.sex=sex;
}
public void speak(){
super.speak();
System.out.println("我的性别是"+sex);
}
}
又好几天没有写博客啦,尽量以后少打游戏,多抽点时间写点文章。
这篇代码主要是一个宠物店类,主要用啦面向对象中继承的思想。先定义啦一个基类pet类,Dog类和pengguin类继承啦petl类。这是学java以来第一次用到面向对象的思想。并学到啦与c++不同的地方。继承的关键字extends,和supper()。
二:老师给的代码如下:
petshop
package jsj.java.exam.test05;
import java.util.Scanner;
public class PetShop {
public static void main(String[] args) {
System.out.println("欢迎来到宠物商店");
System.out.println("请输入要领养的宠物的名字");
Scanner input=new Scanner(System.in);
String name=input.next();
System.out.println("请输入你要领养的宠物的类型:(1 狗狗 2 企鹅)");
int type=input.nextInt();
Master master=new Master();
switch(type){
case 1:System.out.println("请输入狗的品种(1 聪明的拉布拉多犬 2苦苦的雪纳瑞)");
int breed=input.nextInt();
System.out.println("请输入狗的健康值");
int health=input.nextInt();
String s="";
if(breed==1){
s="聪明的拉布拉多犬";
}else{
s="苦苦的雪纳瑞";
}
Dog dog=new Dog(name,s,health,20);
System.out.println("喂养前:");
dog.speak();
master.feed(dog);
System.out.println("喂养后:");
dog.speak();
master.play(dog);
System.out.println("与主人玩耍后");
dog.speak();
break;
case 2:
System.out.println("请输入企鹅的性别(1Q仔 2Q妹)");
int sex=input.nextInt();
System.out.println("请输入企鹅得健康值");
health=input.nextInt();
s="";
if(sex==1){
s="Q仔";
}else{
s="Q妹";
}
Penguin penguin=new Penguin(name, s, health, 30);
penguin.speak();
master.feed(penguin);
System.out.println("喂养后:");
penguin.speak();
master.play(penguin);
System.out.println("与主人玩耍后");
penguin.speak();
}
}
}
package jsj.java.exam.test05;
public abstract class Pet {
protected String name;
protected int health;
protected int love;
public Pet(String name, int health, int love) {
super();
this.name = name;
this.health = health;
this.love = love;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
if (health >= 1 && health <= 100) {
this.health = health;
} else {
this.health = 10;
}
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
protected abstract void speak();
protected abstract void eat();
}
package jsj.java.exam.test05;
public class Penguin extends Pet {
private String sex;
public Penguin(String name, String sex, int health, int love) {
super(name,health,love);
this.sex = sex;
}
public final void speak() {
System.out.println("宠物的自白");
System.out.println("我的名字叫" + getName() + "我的健康状况是" + getHealth() + "我的性别是"
+ sex + "我与主人的亲密度为" + getLove());
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
protected void eat() {
super.health=super.health+6;
}
public void swimming(){
System.out.println("游泳");
super.health=super.health-3;
super.love=super.love+5;
}
}
package jsj.java.exam.test05;
public class Dog extends Pet {
private String breed;
public Dog(String name, String breed, int health, int love) {
super(name,health,love);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void speak() {
System.out.println("宠物的自白");
System.out.println("我的名字叫" + getName() + "我的健康状况是" + getHealth()
+ "我的品种是" + breed + "我与主人的亲密度为" + getLove());
}
@Override
protected void eat() {
super.health=super.health+3;
}
public void catchFlyingDisk(){
System.out.println("正在玩接飞盘的游戏");
super.health=super.health-5;
super.love=super.love+5;
}
}
Master.java
package jsj.java.exam.test05;
public class Master {
public void feed(Pet pet){
pet.eat();
}
public void play(Pet pet){
if(pet instanceof Dog){
((Dog) pet).catchFlyingDisk();
}else if(pet instanceof Penguin){
((Penguin) pet).swimming();
}
}
}
相关知识
用java编的实现宠物店领养宠物的功能
毕业论文题目:基于Java的宠物领养系统设计与实现摘要:本论文介绍了一种基于Java的宠物领养系统的设计与实现方法。该系统能够有效地管理宠物信息、提高领养效率、降低领养成本,同时提供安全、可靠的宠
java计算机毕业设计ssm宠物店管理系统
java毕设安卓宠物店管理网站(开题+源码)
案例27:基于Java宠物领养系统开题报告设计
Java宠物网站系统的设计与实现
Java基于JAVA语言的宠物寄养管理(开题+源码)
宠物领养系统的设计与实现 计算机专业毕业设计源码46903
宠物领养系统的设计与实现 计算机毕设源码46903
基于智能推荐的宠物之家网站设计与实现(开题报告+源码)
网址: 用java编的实现宠物店领养宠物的功能 https://m.mcbbbk.com/newsview31425.html
上一篇: 五一假期前宠物寄养市场火爆 “铲 |
下一篇: 加盟宠物店需要多少钱 加盟宠物店 |