一、父类–Pet
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建
的。
public class Pet {
private String name;
private int age;
private int health;
public Pet(int health) {
this.health = health;
}
public Pet() {
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
if (this.health>=100){
this.health=100;
System.out.println("人..人家真的吃不下了");
}
this.health = health;
}
public void eat(String food){
System.out.println("宠物吃"+food);
}
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;
}
}
二、子类–DogLady
代码如下(示例):
public class DogLady extends Pet {
private int love;
public DogLady(int health) {
super(health);
}
public DogLady() {
}
//宠物玩耍
public void play(){
System.out.<

本文介绍了使用Java构建一个电子宠物系统,包括父类Pet、子类DogLady和CatLady、主人类Master以及功能测试类Test。通过面向对象的编程概念如封装、继承和多态进行实现,适合初学者实践。
最低0.47元/天 解锁文章
5750

被折叠的 条评论
为什么被折叠?



