面向对象编程(Java)--动物继承

abstract class Animal{
 protected boolean mammal;
 protected boolean carnivorous;
 protected int mood;
 
 Animal(boolean mammal,boolean carnivorous){
  this.mammal = mammal;
  this.carnivorous = carnivorous;
 }

 abstract void isMammal();
 abstract void isCarnivorous();
 void setMood(int newValue){
  mood = newValue;
 }
 abstract void getMood();
 abstract void sayHello();
}

interface LandAnimal{
 void getNumberOfLegs();
}

interface WaterAnimal{
 void hasGills();
 void laysEggs();
}

class Dog extends Animal implements LandAnimal{
 private int legs;
 Dog(){
  super(true,true);
  legs = 4;
 }
 void isMammal(){
  System.out.println("狗" + mammal + "哺乳动物");
 }
 void isCarnivorous(){
  System.out.println("狗" + mammal + "肉食动物");
 }
 void setMood(int newValue){
  mood = newValue;
 }
 void getMood(){
  if(mood == 1)
   System.out.println("狗高兴时旺旺叫...");
  else
   System.out.println("狗不高兴时乱咬人...");
 }
 void sayHello(){
  System.out.println("狗跟人打招呼是甩甩尾...");
 }

 public void getNumberOfLegs(){
  System.out.println("狗有" + legs + "条腿");
 }

 void showInfo(){
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
  isMammal();
  isCarnivorous();
  getNumberOfLegs();
  getMood();
  sayHello();
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
 }
}

class Cat extends Animal implements LandAnimal{
 private int legs;
 Cat(){
  super(true,true);
  legs = 4;
 }
 void isMammal(){
  System.out.println("猫" + mammal + "哺乳动物");
 }
 void isCarnivorous(){
  System.out.println("猫" + mammal + "肉食动物");
 }
 void setMood(int newValue){
  mood = newValue;
 }
 void getMood(){
  if(mood == 1)
   System.out.println("猫高兴时会爬树...");
  else
   System.out.println("猫不高兴时捉老鼠...");
 }
 void sayHello(){
  System.out.println("猫跟人打招呼是猫咪叫...");
 }

 public void getNumberOfLegs(){
  System.out.println("猫有" + legs + "条腿");
 }

 void showInfo(){
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
  isMammal();
  isCarnivorous();
  getNumberOfLegs();
  getMood();
  sayHello();
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
 }
}

class Frog extends Animal implements WaterAnimal{
 private int legs;
 Frog(){
  super(false,false);
  legs = 2;
 }
 void isMammal(){
  System.out.println("青蛙" + mammal + "哺乳动物");
 }
 void isCarnivorous(){
  System.out.println("青蛙" + mammal + "肉食动物");
 }
 void setMood(int newValue){
  mood = newValue;
 }
 void getMood(){
  if(mood == 1)
   System.out.println("青蛙高兴时会游泳...");
  else
   System.out.println("青蛙不高兴时会潜水...");
 }
 void sayHello(){
  System.out.println("青蛙跟人打招呼是呱呱叫...");
 }

 public void getNumberOfLegs(){
  System.out.println("青蛙有" + legs + "条腿");
 }
 public void hasGills(){
  System.out.println("青蛙有鳃");
 }
 public void laysEggs(){
  System.out.println("青蛙是卵生的");
 }

 void showInfo(){
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
  isMammal();
  isCarnivorous();
  getNumberOfLegs();
  getMood();
  hasGills();
  laysEggs();
  sayHello();
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
 }
}

class Bird extends Animal implements LandAnimal{
 private int legs;
 Bird(){
  super(false,false);
  legs = 2;
 }
 void isMammal(){
  System.out.println("鸟" + mammal + "哺乳动物");
 }
 void isCarnivorous(){
  System.out.println("鸟" + mammal + "肉食动物");
 }
 void setMood(int newValue){
  mood = newValue;
 }
 void getMood(){
  if(mood == 1)
   System.out.println("鸟高兴时会飞天...");
  else
   System.out.println("鸟不高兴时下地...");
 }
 void sayHello(){
  System.out.println("鸟跟人打招呼是唱歌...");
 }

 public void getNumberOfLegs(){
  System.out.println("鸟有" + legs + "条腿");
 }

 void showInfo(){
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
  isMammal();
  isCarnivorous();
  getNumberOfLegs();
  getMood();
  sayHello();
  System.out.println("-- * -- * -- * -- * -- * -- * -- * -- * --");
 }
}

class Test{
 public static void main(String args[]){
  Dog d = new Dog();
  d.setMood(1);
  d.showInfo();
  
  Cat c = new Cat();
  c.setMood(1);
  c.showInfo();

  Frog f = new Frog();
  f.setMood(1);
  f.showInfo();
  
  Bird b = new Bird();
  b.setMood(2);
  b.showInfo();
 }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值