展开全部
public class Animal
{
public String name;
public int legs;
public String sound;
public Animal(String name, int legs, String sound){
e68a8462616964757a686964616f31333332623262this.name = name;
this.legs = legs;
this.sound = sound;
}
public String toString(){
return name + "\t" + legs + "\t" + sound;
}
public static void main(String[]args)
{
//init
Animal[] animals = new Animal[3];
animals[0] = new Animal("加菲猫",4, "喵喵喵");
animals[1] = new Animal("唐小鸭",2, "嘎嘎嘎");
animals[2] = new Animal("海豚奇奇",0, "海豚音");
byte[] b = new byte[1024];
String[] names = {"猫", "鸭子", "海豚"};
int[] legs = {4,2,0};
String name;
int leg;
String sound;
while(true){
try{
System.out.println("动物名称\t腿的条数\t动物叫");
for (int i = 0; i
System.out.println(animals[i].toString());
}
System.out.println("0-修改,其他数字-退出");
int n = System.in.read(b);
String s = new String(b,0,n);
int oper = Integer.parseInt(s.trim());
if (oper != 0) {
System.out.println("退出!");
return;
}
for (int i = 0; i
System.out.println("请输入"+names[i]+"的名字");
n = System.in.read(b);
s = new String(b,0,n);
name = s.trim();
if (name.length() == 0) {
throw new Exception(names[i]+"必须有名字");
}
System.out.println("请输入"+names[i]+"的腿数");
n = System.in.read(b);
s = new String(b,0,n);
leg = Integer.parseInt(s.trim());
if (leg != legs[i]) {
throw new Exception(names[i]+"必须有"+legs[i]+"条腿");
}
System.out.println("请输入"+names[i]+"的叫声");
n = System.in.read(b);
s = new String(b,0,n);
sound = s.trim();
if (sound.length() == 0) {
throw new Exception(names[i]+"必须有声音");
}
animals[i].name = name;
animals[i].legs = leg;
animals[i].sound = sound;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
凑合写的....
instanceof 不难吧...就是验证类是否为类或者父类的实例...
我看你ppt要求的是自定义异常抛出,接口继承应该很简单,抛出异常参考我的代码即可..