public class Animal
创新互联-专业网站定制、快速模板网站建设、高性价比梁园网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式梁园网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖梁园地区。费用合理售后完善,10多年实体公司更值得信赖。
{
public String name;
public int legs;
public String sound;
public Animal(String name, int legs, String sound){
this.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 animals.length; 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 animals.length; 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要求的是自定义异常抛出,接口继承应该很简单,抛出异常参考我的代码即可..
public class Monkey {
private String color;
private String sex;
public Monkey(String color,String sex) {
this.color = color;
this.sex = sex;
}
public Monkey(String color) {
this.color = color;
this.sex = "公";
}
public Monkey(){}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int add(int p1,int p2,int p3) {
return p1+p2+p3;
}
public int add(int p1,int p2) {
return p1+p2;
}
}
public class Zoo {
public static void main(String[] args) {
Monkey g = new Monkey("黑");
Monkey l = new Monkey("红","母");
int p1 = g.add(1, 2);
int p2 = l.add(1, 2, 3);
System.out.println(g.getSex()+":"+p1);
System.out.println(l.getSex()+":"+p2);
}
}
我写了一个,内容比较简单的。代码如下:public class AnimalTest {
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}