Java喂狗代码 喂狗指令
java程序 一个主人有两只宠物,一条狗一只猫,狗叫旺财,猫叫小花,分别给宠物喂食
public class Person {
成都创新互联是一家专业提供赤城企业网站建设,专注与成都做网站、成都网站设计、HTML5建站、小程序制作等业务。10年已为赤城众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
public static void main(String args[]) {
Animal a1 = new Animal("猫","小花","鱼");
Animal a2 = new Animal("狗","旺财","骨头");
a1.eat();a2.ert();
}
}
class Animal {
String species;
String animalName;
String foodName;
Animal(String species,String animalName,String foodName) {
this.species = species;
this.animalNam e= animalName;
this.foodName = foodName;
}
public void eat() {
System.out.print("我的"+species+","+animalName +" 吃了 :"+foodName);
}
}
//我也才学java不到一个月,我就能理解这么多
//还可以写继承的。亦可以写成多线程的。
//继承就是把Animal类当父类,再写两个类继承它。这里又可以复习多态,哈哈。
//多线程就是同事喂这俩动物。
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代码主人喂宠物吃东西狗只吃骨头猫只吃鱼求代码
class 动物{
public boolean 吃(食物 sw){
}
}
class 狗 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 骨头) return true;
else return false;
}
}
class 猫 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 鱼) return true;
else return false;
}
}
你还需要建立一个 食物 类。
用Java程序完成以下场景(用继承多态):有一个主人(Master类),他养了两只宠物(Pet类)
public class Run {
public static void main(String[] args) {
Master master = new Master();
master.feedDog("鸡骨头");
master.feedCat("鸡骨头");
}
}
class Master {
private Pet mPet;
private Food mFood;
public void feedCat(String food) {
mPet = new Cat();
mFood = new Food(food);
mPet.eat(mFood);
}
public void feedDog(String food) {
mPet = new Dog();
mFood = new Food(food);
mPet.eat(mFood);
}
}
class Dog extends Pet{
@Override
public void eat(Food food) {
System.out.println("正在喂小狗吃"+food.getFood());
if (food.getFood().matches(Food.BONE)) {
System.out.println("小狗正在吃"+food.getFood()+"!");
}else {
System.out.println("但是小狗不喜欢吃"+food.getFood()+"!");
}
}
}
class Cat extends Pet{
@Override
public void eat(Food food) {
System.out.println("正在喂小猫吃"+food.getFood());
if (food.getFood().matches(Food.FISH)) {
System.out.println("小猫正在吃"+food.getFood()+"!");
}else {
System.out.println("但是小猫不喜欢吃"+food.getFood()+"!");
}
}
}
class Food {
public final static String BONE = ".*骨.*";
public final static String FISH = ".*鱼.*";
private String food;
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public Food(String food) {
this.food = food;
}
}
class Pet {
public void eat(Food food) {
}
}
分享题目:Java喂狗代码 喂狗指令
文章来源:http://scyanting.com/article/dogdgsg.html