/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package animal.mammal;
import java.io.*;
/**
*
* @author Administrator
*/
public class MainMammal {
public static void main(String args[]){
EnumMammal En=null;
Integer s=0;
String str;
char t='n';
Mammal m=new Mammal();
BufferedReader br;
InputStreamReader in;
outer: while(true){
System.out.println("choic mammal:");
System.out.print("1.Dog ");
System.out.print("2.Cat ");
System.out.print("3.Horse ");
System.out.print("4.Pig ");
System.out.print("0.exit ");
in=new InputStreamReader(System.in);
br=new BufferedReader(in);
try{
str=br.readLine();
s=new Integer(str);
}catch(IOException e){}
switch(s){
case 1:
m=new DogMammal(1,2);
En=EnumMammal.Dog;break;
case 2:
m=new CatMammal(1,2);
En=EnumMammal.Cat;break;
case 3:
m=new HorseMammal(1,2);
En=EnumMammal.Horse;break;
case 4:
m=new PigMammal(1,2);
En=EnumMammal.Pig;break;
case 0:
break outer;
default:
System.out.println("input error!");
continue;
}
System.out.println("set age:");
in=new InputStreamReader(System.in);
br=new BufferedReader(in);
try{
str=br.readLine();
s=new Integer(str);
}catch(IOException e){}
m.setAge(s);
System.out.println("set weight:");
in=new InputStreamReader(System.in);
br=new BufferedReader(in);
try{
str=br.readLine();
s=new Integer(str);
}catch(IOException e){}
m.setWeight(s);
System.out.println("finish?(Y/N)");
in=new InputStreamReader(System.in);
br=new BufferedReader(in);
try{
t=(char)br.read();
}catch(IOException e){}
if(t=='N'||t=='n')
continue outer;
else{
System.out.println("the animal your chioced is "+En);
System.out.println("Varieties:"+m.getVarieties());
System.out.println("Age:"+m.getAge());
System.out.println("Weight:"+m.getWeight());
}
while(true){
System.out.println("choice action:");
System.out.print("1.Move ");
System.out.print("2.Speak ");
System.out.print("3.Sleep ");
System.out.print("4.Cry ");
System.out.print("5.Wag ");
System.out.print("6.Beg ");
System.out.print("7.choice other animals ");
System.out.print("0.exit ");
in=new InputStreamReader(System.in);
br=new BufferedReader(in);
try{
str=br.readLine();
s=new Integer(str);
}catch(IOException e){}
switch(s){
case 1:m.move();break;
case 2:m.speak();break;
case 3:m.sleep();break;
case 4:m.Cry();break;
case 5:m.Wag();break;
case 6:m.Beg();break;
case 7:continue outer;
case 0:break outer;
default:continue;
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java语言中继承和多态。
共33个文件
class:12个
java:12个
xml:4个
5星 · 超过95%的资源 需积分: 24 74 下载量 68 浏览量
2011-12-22
10:04:59
上传
评论 2
收藏 34KB ZIP 举报
温馨提示
仿真农场 一个哺乳动物类如下所示: public class Mammal{ protected int age = 2; protected int weight = 5; public Mammal(){ System.out.println(“Mammal constructor”); } int getAge(){return age;} void setAge(int a){age = a;} int getWeight(){return weight;} void setWeight(int w){weight = w;} void move(){System.out.println(“Mammal move one step”);} void speak(){System.out.println(“Mammal speak!”);} void sleep(){System.out.println(“shhh, I’m sleeping.”);} } 狗属哺乳动物,且它的属性有品种之分(在哺乳类基础上增加品种数据成员),叫声区别于其他动物(输出“Woof!”),还会摇尾巴(增加成员函数,输出“Tail wagging…”),乞讨食物(增加成员函数,输出“begging for food…”)。 猫、马、猪也属于哺乳动物,其叫声分别为:“Meow!”,“Winnie!”,“Oink!”。 编程分别使各个动物表现为不一样的行为。要求将所有的动物类放置于animal.mammal包下。 定义两个新的类,表示宠物猫和宠物狗。要求:这两个类能输出宠物所属的主人,价格等信息;将这两个类放置于animal.mammal.pet包下。
资源推荐
资源详情
资源评论
收起资源包目录
实验5.zip (33个子文件)
实验5
Experiment5
src
animal
mammal
DogMammal.java 523B
PigMammal.java 381B
HorseMammal.java 389B
Mammal.java 874B
CatMammal.java 381B
MainMammal.java 4KB
EnumMammal.java 202B
pet
pet.java 452B
dogPet.java 2KB
catPet.java 2KB
InterPet.java 575B
petMain.java 4KB
build
classes
.netbeans_automatic_build 0B
animal
mammal
EnumMammal.class 1KB
PigMammal.class 637B
CatMammal.class 637B
DogMammal.class 833B
Mammal.class 2KB
HorseMammal.class 647B
pet
InterPet.class 521B
petMain.class 3KB
catPet.class 3KB
pet.class 506B
dogPet.class 3KB
MainMammal.class 3KB
experiment5
nbproject
genfiles.properties 475B
build-impl.xml 55KB
project.properties 2KB
project.xml 519B
private
private.properties 225B
private.xml 211B
build.xml 4KB
manifest.mf 85B
共 33 条
- 1
资源评论
- xzcpp2014-05-31不错 慢慢学习中。。。
- shangbingxin2017-10-20内容完整,不错
- eiwoqu2014-12-09java的继承和多态是学习面向对象必学的内容
fanfangogo90
- 粉丝: 2
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功