java 宠物的自白
时间: 2024-01-02 20:23:03 浏览: 102
Practical.API.Design.Confessions.of.a.Java.Framework
根据提供的引用内容,以下是Java宠物的自白的实现:
```java
import java.util.Scanner;
public class PetShop {
public static void main(String[] args) {
System.out.println("欢迎来到宠物店");
System.out.println("请输入要领养的宠物的名字");
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println("请输入宠物的健康值(1-100)");
int num = sc.nextInt();
System.out.println("请输入与主人的亲密度");
int qinmidu = sc.nextInt();
System.out.println("请输入你要领养的宠物类型:(1 狗狗;2企鹅)");
int z = sc.nextInt();
// 调用Master函数
Master master = new Master(name, num, qinmidu, z);
}
}
public class Master {
Master(String name, int num, int qinmidu, int z) {
// 选择宠物
if (z == 1) {
Dog dog = new Dog(name, num, qinmidu);
} else if (z == 2) {
Penguin penguin = new Penguin(name, num, qinmidu);
} else {
System.out.println("暂无其他宠物,请尽情期待!");
}
}
}
public class Dog {
private String name;
private int health;
private int intimacy;
Dog(String name, int health, int intimacy) {
this.name = name;
this.health = health;
this.intimacy = intimacy;
}
public void selfIntroduction() {
System.out.println("大家好,我是一只狗狗");
System.out.println("我的名字是" + name);
System.out.println("我的健康值是" + health);
System.out.println("与主人的亲密度是" + intimacy);
}
}
public class Penguin {
private String name;
private int health;
private int intimacy;
Penguin(String name, int health, int intimacy) {
this.name = name;
this.health = health;
this.intimacy = intimacy;
}
public void selfIntroduction() {
System.out.println("大家好,我是一只企鹅");
System.out.println("我的名字是" + name);
System.out.println("我的健康值是" + health);
System.out.println("与主人的亲密度是" + intimacy);
}
}
```
阅读全文