public?class?Person?{
成都創(chuàng)新互聯(lián)是一家專業(yè)提供赤城企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為赤城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
public?static?void?main(String?args[])?{
Animal?a1?=?new?Animal("貓","小花","魚");
Animal?a2?=?new?Animal("狗","旺財(cái)","骨頭");
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);
}
}
//我也才學(xué)java不到一個(gè)月,我就能理解這么多
//還可以寫繼承的。亦可以寫成多線程的。
//繼承就是把Animal類當(dāng)父類,再寫兩個(gè)類繼承它。這里又可以復(fù)習(xí)多態(tài),哈哈。
//多線程就是同事喂這倆動(dòng)物。
/*
animal是個(gè)抽象方法,Cat?和Dog?extends?這個(gè)就是用的多態(tài)
*/
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!");
}
}
class 動(dòng)物{
public boolean 吃(食物 sw){
}
}
class 狗 extends 動(dòng)物{
public boolean 吃(食物 sw){
if(sw.種類 == 骨頭) return true;
else return false;
}
}
class 貓 extends 動(dòng)物{
public boolean 吃(食物 sw){
if(sw.種類 == 魚) return true;
else return false;
}
}
你還需要建立一個(gè) 食物 類。
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)?{
}
}