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!");
();
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;
System.out.println("再见!");
default:
System.out.println("输⼊有误!请重新输⼊!");
}
}
}
}
}
View Code
第⼆阶段考虑情绪影响动物打招呼的⽅式
扩充Animal类、Dog类、Cat类和Frog类,增加Animal类的mood属性,并实现SayHello⽅法的多态性;扩充main⽅法;
1package text;
2import java.util.Scanner;
3
4abstract class Animal
5 {
6protected boolean mammal=true; // 哺乳动物
7protected boolean carnivorous=true; //⾷⾁动物
8public static final int scared=2; //情绪不好
9public static final int comforted=1; //情绪好
10protected int mood=comforted;
11
12public boolean ismammal(){return (mammal);}
13public boolean iscarnivorous(){return(carnivorous);}
14abstract String SayHello();
15abstract String SayHello(int moodval);
16public void setMood(int newValue){ mood=newValue;}
17public int getMood(){ return(mood); };
18 }
19class Dog extends Animal
20 {
21 String name="dog";
22public String SayHello(){return "摇摇尾巴";};
23public String SayHello(int moodval){
24this.setMood(moodval);
25switch(mood){
26case scared:
27return("呜呜叫");
28case comforted:
29return("汪汪叫");
30 }
31return ("摇摇尾巴");
32 }
33 }
34
35class Cat extends Animal
36 {
37 String name="cat";
38public String SayHello(){return "喵喵叫";};
39public String SayHello(int moodval){
40this.setMood(moodval);
41switch(mood){
42case scared:
43return("嘶嘶");
44case comforted:
45return("咕噜咕噜");
46 }
47return ("喵喵叫");
48 }
49 }
50
51class Frog extends Animal {
52 String name="Frog";
53public String SayHello(){return "呱呱";};
54public String SayHello(int moodval)
55 {
56this.setMood(moodval);
57switch(mood){
58case scared:
59return("扑通⼀声跳⼊⽔中");
60case comforted:
61return("呱呱呱");
63return ("呱呱");
64 }
65
66 }
67
68
69public class Main
70 {
71public 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(" ~欢迎光临索迪动物园~ ");
77for(;;){
78 System.out.println("请输⼊动物名称:dog,cat或者frog ,退出exit!");
79 String name=new String();
80 @SuppressWarnings("resource")
81 Scanner sc=new Scanner(System.in);
82 ();
83 System.out.println("输⼊动物:"+name);
84switch(name)
85 {
86case "dog":
87 System.out.println("狗通常情况下,跟⼈的打招呼⽅式为:" + dog.SayHello()); break; 88case "cat":
89 System.out.println("猫通常情况下,跟⼈的打招呼⽅式为:" + cat.SayHello());break; 90case "frog":
91 System.out.println("青蛙通常情况下,跟⼈的打招呼⽅式为:" + frog.SayHello());break; 92case "exit":
93 System.out.println("再见!");
94 it(0);
95default:
96 System.out.println("输⼊有误!请重新输⼊!");
97 }
98 System.out.println("请输⼊动物的⼼情:1-⼼情好,2-⼼情不好");
99int i;
100 @SuppressWarnings("resource")
101 Scanner sc1=new Scanner(System.in);
102 Int();
103switch(i){
104case 1: System.out.println("它⼼情是:⼼情好"); break;
105case 2: System.out.println("它⼼情是:⼼情不好");break;
106default:
107 System.out.println("输⼊有误!请重新输⼊!");
108 }
109switch(name){
110case "dog":if(i==1){
111 System.out.println("狗⼼情好:"+dog.SayHello(Animalforted));
112 }else if(i==2){
113 System.out.println("狗⼼情不好:"+dog.SayHello(Animal.scared));
114 }else{
115 System.out.println("输⼊错误!");
116 }break;
117case "cat": if (i==1){
118 System.out.println("猫⼼情好:"+cat.SayHello(Animalforted));
119 }else if(i==2){
120 System.out.println("猫⼼情不好:"+cat.SayHello(Animal.scared));
121 }else{
122 System.out.println("输⼊错误!");
123 }break;
124case "frog":if (i==1){
125 System.out.println("青蛙⼼情好:"+frog.SayHello(Animalforted));
126 }else if(i==2){
127 System.out.println("青蛙⼼情不好:"+frog.SayHello(Animal.scared));
128 }else
129 {
130 System.out.println("输⼊错误!");
131 }break;
132default:
133 System.out.println("输⼊有误!请重新输⼊!");break;
134 }
135 }
136 }
137
138
139 }
140
141
142
View Code
第三阶段考虑陆⽣动物和⽔⽣动物
定义LandAnimal接⼝和WaterAnimal接⼝;
扩充Dog类、Cat类和Frog类,使其实现相应的接⼝;
扩充main⽅法;
1package text;
2import java.util.Scanner;
3abstract class Animal
4 {
5protected boolean mammal=true; // 哺乳动物
6protected boolean carnivorous=true; //⾷⾁动物
7public static final int scared=0; //情绪不好
8public static final int comforted=1; //情绪好
9protected int mood=comforted;
10public boolean ismammal(){return (mammal);}
11public boolean iscarnivorous(){return(carnivorous);}
12abstract String SayHello();
13abstract String SayHello(int moodval);
14public void setMood(int newValue){ mood=newValue;}
15public int getMood(){ return(mood); };
16 }
17interface LandAnimal
18 {
19public int getNumberOfLegs();//返回有多少条腿
20 }
21interface WaterAnimal
22 {
23public boolean isGillFlag();//返回是否有腮
24public boolean isLaysEggs();//返回是否产卵
25 }
26class Dog extends Animal implements LandAnimal
27 {
java面向对象的特征有哪些方面28private int numberOfLegs=4;
29public String SayHello(){return "摇摇尾巴";};
30public String SayHello(int moodval){
31this.setMood(moodval);
32switch(mood){
33case scared:
34return("呜呜叫");
35case comforted:
36return("汪汪叫");
37 }
38return ("摇摇尾巴");
39 }
40public int getNumberOfLegs()
41 {
42return numberOfLegs;
43 }
44 }
45class Cat extends Animal implements LandAnimal
46 {
47private int numberOfLegs=4;
48public String SayHello(){return "喵喵叫";};
49public String SayHello(int moodval){
50this.setMood(moodval);
51switch(mood){
52case scared:
53return("嘶嘶");
54case comforted:
55return("咕噜咕噜");
56 }
57return ("喵喵叫");
58 }
59public int getNumberOfLegs()
60 {
61return numberOfLegs;
62 }
63 }
64class Frog extends Animal implements LandAnimal, WaterAnimal 65 {
66private int numberOfLegs=4;
67private boolean gillFlag = true;
68public Frog()
69 {
70 mammal=false;
71 carnivorous=false;
72 }
73public String SayHello(){return "呱呱";};
74public String SayHello(int moodval){
75this.setMood(moodval);
76switch(mood){
77case scared:
78return("扑通⼀声跳⼊⽔中");
79case comforted:
80return("呱呱呱");
81 }
82return ("呱呱");
83 }
84public int getNumberOfLegs()
85 {
86return numberOfLegs;
87 }
88public boolean isGillFlag()
89 { return gillFlag; }
90public boolean isLaysEggs()
91 { return gillFlag; }
92 }
93public class Main
94 {
95public 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(" ~欢迎光临索迪动物园~ ");
101for(;;){
102 System.out.println("请输⼊动物名称:dog,cat或者frog ,退出exit!");
103 String name=new String();
104 @SuppressWarnings("resource")
105 Scanner sc=new Scanner(System.in);
106 ();
107switch(name)
108 {
109case "dog":
110 System.out.println("该动物是陆⽣动物,它有"+NumberOfLegs()+"条腿");
111 System.out.println("狗通常情况下,跟⼈的打招呼⽅式为:" + dog.SayHello()); break;
112case "cat":
113 System.out.println("该动物是陆⽣动物,它有"+NumberOfLegs()+"条腿");
114 System.out.println("猫通常情况下,跟⼈的打招呼⽅式为:" + cat.SayHello());break;
115case "frog":if(frog.isLaysEggs()&&frog.isGillFlag())
116 System.out.println("该动物是陆⽣动物,它有"+NumberOfLegs()+"条腿。"+"有腮"+","+"产卵"); 117 System.out.println("青蛙通常情况下,跟⼈的打招呼⽅式为:" + frog.SayHello());break;
118case "exit":
119 System.out.println("再见!");
120 it(0);
121default:
122 System.out.println("输⼊有误!请重新输⼊!");
123 }
124 System.out.println("请输⼊动物的⼼情:1-⼼情好,2-⼼情不好");
125int i;
126 @SuppressWarnings("resource")
127 Scanner sc1=new Scanner(System.in);
128 Int();
129switch(i){
130case 1: System.out.println("它⼼情是:⼼情好"); break;
131case 2: System.out.println("它⼼情是:⼼情不好");break;
132default:
133 System.out.println("输⼊有误!请重新输⼊!");
134 }
135switch(name){
136case "dog":if(i==1){
137 System.out.println("狗⼼情好:"+dog.SayHello(Animalforted));
138 }else if(i==2){
139 System.out.println("狗⼼情不好:"+dog.SayHello(Animal.scared));
140 }else{
141 System.out.println("输⼊错误!");
142 }break;
143case "cat": if (i==1){
144 System.out.println("猫⼼情好:"+cat.SayHello(Animalforted));
145 }else if(i==2){
146 System.out.println("猫⼼情不好:"+cat.SayHello(Animal.scared));
147 }else{
148 System.out.println("输⼊错误!");
149 }break;
150case "frog":if (i==1){
151 System.out.println("青蛙⼼情好:"+frog.SayHello(Animalforted));
152 }else if(i==2){
153 System.out.println("青蛙⼼情不好:"+frog.SayHello(Animal.scared));
154 }else
155 {
156 System.out.println("输⼊错误!");
157 }break;
158default:
159 System.out.println("输⼊有误!请重新输⼊!");break;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论