java实现动物特性的面向对象的描述(三个阶段)
动物特性描述
狗生活在陆地上(是一种陆生动物),既是哺乳类的也是肉食性的。狗通常的时候和人打招呼会通过“摇摇尾巴”,在被抚摸感到舒服的时候,会“旺旺叫”,而在受到惊吓情绪烦躁时,会发出“呜呜”声;
猫也生活在陆地上(是一种陆生动物),既是哺乳类的也是肉食性的。猫通常的时候和人打招呼会发出“喵~”的声音,在被抚摸情绪很好时,会发出“咕噜咕噜”声,而在受到惊吓时,会发出“嘶嘶”声;
青蛙是一种两栖动物(既是水生动物也是陆生动物),既不是哺乳类的也不是肉食性的,属于卵生。当青蛙情绪好的时候,会在岸边“呱呱呱”的唱歌,而在受到惊吓时,会“扑通一声跳入水中”;
分为三阶段
第一阶段:不考虑情绪影响动物打招 呼的方式
第二阶段:考虑情绪影响动物打招呼的方式
第三阶段:考虑陆生动物和水生动物
第一阶段:先要考虑抽象问题描述中对象,分析对象的特征和动作,从中找出抽象类的属性和方法画出类的结构图
编写Animal类,没有mood属性,只有一种SayHello方法;
编写Dog类、Cat类和Frog类,分别继承自Animal类,实现与Animal类不同的功能;
编写main方法,分别实例化以上三个类的三个对象,测试类方法实现的正确性;
第一阶段源代码
package text; import java.util.Scanner; abstract class Animal { protected boolean mammal=true; // 哺乳动物 protected boolean carnivorous=true; //食肉动物 public boolean ismammal(){return (mammal);} public boolean iscarnivorous(){return(carnivorous);} abstract String SayHello(); } class Dog extends Animal { String name="dog"; public String SayHello(){return "摇摇尾巴";} } class Cat extends Animal { String name="cat"; public String SayHello(){return "喵喵叫";} } class Frog extends Animal { String name="Frog"; public String SayHello(){return "呱呱";} } public class Main { public static void main(String args[]) { Dog dog=new Dog(); Cat cat=new Cat(); Frog frog=new Frog(); System.out.println(" ~欢迎光临索迪动物园~ "); for(;;) { String name=new String(); @SuppressWarnings("resource") Scanner sc=new Scanner(System.in); System.out.println("请输入动物名称:dog,cat或者frog ,退出exit!"); name=sc.next(); if(name!="dog"||name=="cat"||name=="frog"||name=="exit") { switch(name) { case "dog": System.out.println("狗通常情况下,跟人的打招呼方式为:" + dog.SayHello());break; case "cat": System.out.println("猫通常情况下,跟人的打招呼方式为:" + cat.SayHello());break; case "frog": System.out.println("青蛙通常情况下,跟人的打招呼方式为:" + frog.SayHello());break; case "exit": System.out.println("再见!"); System.exit(0); default: System.out.println("输入有误 !请重新输入!"); } } } } } View Code
第二阶段考虑情绪影响动物打招呼的方式
扩充Animal类、Dog类、Cat类和Frog类,增加Animal类的mood属性,并实现SayHello方法的多态性;
扩充main方法;
1 package text; 2 import java.util.Scanner; 3 4 abstract class Animal 5 { 6 protected boolean mammal=true; // 哺乳动物 7 protected boolean carnivorous=true; //食肉动物 8 public static final int scared=2; //情绪不好 9 public static final int comforted=1; //情绪好 10 protected int mood=comforted; 11 12 public boolean ismammal(){return (mammal);} 13 public boolean iscarnivorous(){return(carnivorous);} 14 abstract String SayHello(); 15 abstract String SayHello(int moodval); 16 public void setMood(int newValue){ mood=newValue;} 17 public int getMood(){ return(mood); }; 18 } 19 class Dog extends Animal 20 { 21 String name="dog"; 22 public String SayHello(){return "摇摇尾巴";}; 23 public String SayHello(int moodval){ 24 this.setMood(moodval); 25 switch(mood){ 26 case scared: 27 return("呜呜叫"); 28 case comforted: 29 return("汪汪叫"); 30 } 31 return ("摇摇尾巴"); 32 } 33 } 34 35 class Cat extends Animal 36 { 37 String name="cat"; 38 public String SayHello(){return "喵喵叫";}; 39 public String SayHello(int moodval){ 40 this.setMood(moodval); 41 switch(mood){ 42 case scared: 43 return("嘶嘶"); 44 case comforted: 45 return("咕噜咕噜"); 46 } 47 return ("喵喵叫"); 48 } 49 } 50 51 class Frog extends Animal { 52 String name="Frog"; 53 public String SayHello(){return "呱呱";}; 54 public String SayHello(int moodval) 55 { 56 this.setMood(moodval); 57 switch(mood){ 58 case scared: 59 return("扑通一声跳入水中"); 60 case comforted: 61 return("呱呱呱"); 62 } 63 return ("呱呱"); 64 } 65 66 } 67 68 69 public class Main 70 { 71 public static void main(String args[]) 72 { 73 Dog dog=new Dog(); 74 Cat cat=new Cat(); 75 Frog frog=new Frog(); 76 System.out.println(" ~欢迎光临索迪动物园~ "); 77 for(;;){ 78 System.out.println("请输入动物名称:dog,cat或者frog ,退出exit!"); 79 String name=new String(); 80 @SuppressWarnings("resource") 81 Scanner sc=new Scanner(System.in); 82 name=sc.next(); 83 System.out.println("输入动物:"+name); 84 switch(name) 85 { 86 case "dog": 87 System.out.println("狗通常情况下,跟人的打招呼方式为:" + dog.SayHello()); break; 88 case "cat": 89 System.out.println("猫通常情况下,跟人的打招呼方式为:" + cat.SayHello());break; 90 case "frog": 91 System.out.println("青蛙通常情况下,跟人的打招呼方式为:" + frog.SayHello());break; 92 case "exit": 93 System.out.println("再见!"); 94 System.exit(0); 95 default: 96 System.out.println("输入有误 !请重新输入!"); 97 } 98 System.out.println("请输入动物的心情:1-心情好,2-心情不好"); 99 int i; 100 @SuppressWarnings("resource") 101 Scanner sc1=new Scanner(System.in); 102 i=sc1.nextInt(); 103 switch(i){ 104 case 1: System.out.println("它心情是:心情好"); break; 105 case 2: System.out.println("它心情是:心情不好");break; 106 default: 107 System.out.println("输入有误 !请重新输入!"); 108 } 109 switch(name){ 110 case "dog":if(i==1){ 111 System.out.println("狗心情好:"+dog.SayHello(Animal.comforted)); 112 }else if(i==2){ 113 System.out.println("狗心情不好:"+dog.SayHello(Animal.scared)); 114 }else{ 115 System.out.println("输入错误!"); 116 }break; 117 case "cat": if (i==1){ 118 System.out.println("猫心情好:"+cat.SayHello(Animal.comforted)); 119 }else if(i==2){ 120 System.out.println("猫心情不好:"+cat.SayHello(Animal.scared)); 121 }else{ 122 System.out.println("输入错误!"); 123 }break; 124 case "frog":if (i==1){ 125 System.out.println("青蛙心情好:"+frog.SayHello(Animal.comforted)); 126 }else if(i==2){ 127 System.out.println("青蛙心情不好:"+frog.SayHello(Animal.scared)); 128 }else 129 { 130 System.out.println("输入错误!"); 131 }break; 132 default: 133 System.out.println("输入有误 !请重新输入!");break; 134 } 135 } 136 } 137 138 139 } 140 141 142
第三阶段考虑陆生动物和水生动物
定义LandAnimal接口和WaterAnimal接口;
扩充Dog类、Cat类和Frog类,使其实现相应的接口;
扩充main方法;
1 package text; 2 import java.util.Scanner; 3 abstract class Animal 4 { 5 protected boolean mammal=true; // 哺乳动物 6 protected boolean carnivorous=true; //食肉动物 7 public static final int scared=0; //情绪不好 8 public static final int comforted=1; //情绪好 9 protected int mood=comforted; 10 public boolean ismammal(){return (mammal);} 11 public boolean iscarnivorous(){return(carnivorous);} 12 abstract String SayHello(); 13 abstract String SayHello(int moodval); 14 public void setMood(int newValue){ mood=newValue;} 15 public int getMood(){ return(mood); }; 16 } 17 interface LandAnimal 18 { 19 public int getNumberOfLegs();//返回有多少条腿 20 } 21 interface WaterAnimal 22 { 23 public boolean isGillFlag();//返回是否有腮 24 public boolean isLaysEggs();//返回是否产卵 25 } 26 class Dog extends Animal implements LandAnimal 27 { 28 private int numberOfLegs=4; 29 public String SayHello(){return "摇摇尾巴";}; 30 public String SayHello(int moodval){ 31 this.setMood(moodval); 32 switch(mood){ 33 case scared: 34 return("呜呜叫"); 35 case comforted: 36 return("汪汪叫"); 37 } 38 return ("摇摇尾巴"); 39 } 40 public int getNumberOfLegs() 41 { 42 return numberOfLegs; 43 } 44 } 45 class Cat extends Animal implements LandAnimal 46 { 47 private int numberOfLegs=4; 48 public String SayHello(){return "喵喵叫";}; 49 public String SayHello(int moodval){ 50 this.setMood(moodval); 51 switch(mood){ 52 case scared: 53 return("嘶嘶"); 54 case comforted: 55 return("咕噜咕噜"); 56 } 57 return ("喵喵叫"); 58 } 59 public int getNumberOfLegs() 60 { 61 return numberOfLegs; 62 } 63 } 64 class Frog extends Animal implements LandAnimal, WaterAnimal 65 { 66 private int numberOfLegs=4; 67 private boolean gillFlag = true; 68 public Frog() 69 { 70 mammal=false; 71 carnivorous=false; 72 } 73 public String SayHello(){return "呱呱";}; 74 public String SayHello(int moodval){ 75 this.setMood(moodval); 76 switch(mood){ 77 case scared: 78 return("扑通一声跳入水中"); 79 case comforted: 80 return("呱呱呱"); 81 } 82 return ("呱呱"); 83 } 84 public int getNumberOfLegs() 85 { 86 return numberOfLegs; 87 } 88 public boolean isGillFlag() 89 { return gillFlag; } 90 public boolean isLaysEggs() 91 { return gillFlag; } 92 } 93 public class Main 94 { 95 public static void main(String args[]) 96 { 97 Dog dog=new Dog(); 98 Cat cat=new Cat(); 99 Frog frog=new Frog(); 100 System.out.println(" ~欢迎光临索迪动物园~ "); 101 for(;;){ 102 System.out.println("请输入动物名称:dog,cat或者frog ,退出exit!"); 103 String name=new String(); 104 @SuppressWarnings("resource") 105 Scanner sc=new Scanner(System.in); 106 name=sc.next(); 107 switch(name) 108 { 109 case "dog": 110 System.out.println("该动物是陆生动物,它有"+dog.getNumberOfLegs()+"条腿"); 111 System.out.println("狗通常情况下,跟人的打招呼方式为:" + dog.SayHello()); break; 112 case "cat": 113 System.out.println("该动物是陆生动物,它有"+cat.getNumberOfLegs()+"条腿"); 114 System.out.println("猫通常情况下,跟人的打招呼方式为:" + cat.SayHello());break; 115 case "frog":if(frog.isLaysEggs()&&frog.isGillFlag()) 116 System.out.println("该动物是陆生动物,它有"+frog.getNumberOfLegs()+"条腿。"+"有腮"+","+"产卵"); 117 System.out.println("青蛙通常情况下,跟人的打招呼方式为:" + frog.SayHello());break; 118 case "exit": 119 System.out.println("再见!"); 120 System.exit(0); 121 default: 122 System.out.println("输入有误 !请重新输入!"); 123 } 124 System.out.println("请输入动物的心情:1-心情好,2-心情不好"); 125 int i; 126 @SuppressWarnings("resource") 127 Scanner sc1=new Scanner(System.in); 128 i=sc1.nextInt(); 129 switch(i){ 130 case 1: System.out.println("它心情是:心情好"); break; 131 case 2: System.out.println("它心情是:心情不好");break; 132 default: 133 System.out.println("输入有误 !请重新输入!"); 134 } 135 switch(name){ 136 case "dog":if(i==1){ 137 System.out.println("狗心情好:"+dog.SayHello(Animal.comforted)); 138 }else if(i==2){ 139 System.out.println("狗心情不好:"+dog.SayHello(Animal.scared)); 140 }else{ 141 System.out.println("输入错误!"); 142 }break; 143 case "cat": if (i==1){ 144 System.out.println("猫心情好:"+cat.SayHello(Animal.comforted)); 145 }else if(i==2){ 146 System.out.println("猫心情不好:"+cat.SayHello(Animal.scared)); 147 }else{ 148 System.out.println("输入错误!"); 149 }break; 150 case "frog":if (i==1){ 151 System.out.println("青蛙心情好:"+frog.SayHello(Animal.comforted)); 152 }else if(i==2){ 153 System.out.println("青蛙心情不好:"+frog.SayHello(Animal.scared)); 154 }else 155 { 156 System.out.println("输入错误!"); 157 }break; 158 default: 159 System.out.println("输入有误 !请重新输入!");break; 160 } 161 } 162 163 } 164 } 165 166
刚开始的时候不知道什么下手,看了网上的一些东西才开始做,以前就没有怎样变过什么代码,java的类的封装,继承,多态。与c++中也一些区别!java不支持多继承,在c++中有多继承的功能,从而java中引入“接口”,从而就实现了多接口!很多地方需要改进,以前都是自己太懒了!没有学博客!以后我要开始我学习之旅!希望大家的指教和帮助!