java宠物动作代码 java宠物类编写
一道java编程题,请各位大神前来解答
代码如下:
成都创新互联公司是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的10多年时间我们累计服务了上千家以及全国政企客户,如水泥搅拌车等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致赞赏。
abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void enjoy();
}
class Cat extends Animal {
private String eyesColor;
public Cat(String name) {
super(name);
}
public String getEyesColor() {
return eyesColor;
}
public void setEyesColor(String eyesColor) {
this.eyesColor = eyesColor;
}
@Override
public void enjoy() {
System.out.println("小猫" + getName() + "高兴的喵喵叫。");
}
}
class Dog extends Animal {
private String furColor;
public Dog(String name) {
super(name);
}
public String getFurColor() {
return furColor;
}
public void setFurColor(String furColor) {
this.furColor = furColor;
}
@Override
public void enjoy() {
System.out.println("狗狗" + getName() + "高兴的摇起了尾巴。");
}
}
class Lady {
private String name;
private Animal pet;
public Lady(String name, Animal pet) {
this.name = name;
this.pet = pet;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Animal getPet() {
return pet;
}
public void setPet(Animal pet) {
this.pet = pet;
}
public void myPetEnjoy() {
pet.enjoy();
}
}
public class App {
public static void main(String[] args) {
Cat cat = new Cat("Jerry");
cat.setEyesColor("蓝色");
Lady lady1 = new Lady("张女士", cat);
lady1.myPetEnjoy();
Dog dog = new Dog("旺财");
dog.setFurColor("黄色");
Lady lady2 = new Lady("王女士", dog);
lady2.myPetEnjoy();
}
}
Java优化宠物系统 求代码 求注解
四个类:Pet Dog Penguin PetTest(测试类)
1、Pet类:
/**
* 宠物类
* Created by LuHuan on 2017/7/27.
*/
public class Pet {
String name = "null";
int health = 100;//健康值
int love = 0;//亲密度
//打印输出宠物信息
public void print() {
System.out.print("我的名字叫" + name + ",我的健康值是:" + health + ",我和主人的亲密程度是" + love + ".");
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getLove() {
return love;
}
}
2、Dog类:
/**
* 狗狗类
* Created by LuHuan on 2017/7/27.
*/
public class Dog extends Pet {
String strain = "拉布拉多犬";//品种
public String getStrain() {
return strain;
}
//重写宠物的自白方法
@Override
public void print() {
super.print();
System.out.println("我是一只" + strain);
}
}
3、Penguin类:
/**
*企鹅类
* Created by LuHuan on 2017/7/27.
*/
public class Penguin extends Pet {
String sex = "Q仔";//性别
public String getSex() {
return sex;
}
//重写宠物的自白方法
@Override
public void print() {
super.print();
System.out.println("我的性别是" + sex);
}
}
4、PetTest测试类:
/**
* 测试类
* Created by LuHuan on 2017/7/27.
*/
public class PetTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "欧欧";
Penguin pgn = new Penguin();
pgn.name = "楠楠";
System.out.println("宠物的自白:");
dog.print();
System.out.println("宠物的自白:");
pgn.print();
}
}
5、输出:
宠物的自白:
我的名字叫欧欧,我的健康值是:100,我和主人的亲密程度是0.我是一只拉布拉多犬
宠物的自白:
我的名字叫楠楠,我的健康值是:100,我和主人的亲密程度是0.我的性别是Q仔
JAVA代码主人喂宠物吃东西狗只吃骨头猫只吃鱼求代码用多态
/*
animal是个抽象方法,Cat 和Dog extends 这个就是用的多态
*/
package Test;
public class Test{
public static void main(String[] args){
Feeder feeder = new Feeder();
feeder.feedAnimals();
}
}
abstract class Animal{
public abstract void eat(String s);
}
class Dog extends Animal{
private final String FOOD = "bone";
@Override
public void eat(String s){
if (s == FOOD)
System.out.println("Dog is eating bones");
else
System.out.println("Not "+this.FOOD+", Dog don't want to eat");
}
}
class Cat extends Animal{
private final String FOOD = "fish";
@Override
public void eat(String s){
if (s == FOOD)
System.out.println("Cat is eating fishes");
else
System.out.println("Not "+this.FOOD+", Cat don't want to eat");
}
}
class Feeder{
private final String[] FOODS = {"fish", "bone", "shit"};
private Animal cat;
private Animal dog;
Feeder(){
dog = new Dog();
cat = new Cat();
}
public void feedAnimals(){
System.out.println("Feeding animals...");
String food;
for(int i = 0; i FOODS.length; i++){
food = FOODS[i];
if(food == "fish")
this.cat.eat(food);
else if(food == "bone")
this.dog.eat(food);
else{
System.out.println("Not Fishes or Bones, is "+ food);
}
}
System.out.println("Done!");
}
}
分享名称:java宠物动作代码 java宠物类编写
转载来于:http://azwzsj.com/article/ddjjjje.html