我遇到一个问题 怎么都解决了 有大神给我看看 ----谢谢
程序代码:
public abstract class Animal { private String name; public Animal() { } public Animal(String name) { this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void call(); } class Animalbass{ Animal[] dolphins=new Animal[4]; }
程序代码:
public class Dog extends Animal implements Refund { private int refund; public Dog(){ } /*public Dog(String name,int refund){ super(name); this.refund=refund; }*/ public int getRefund() { return refund; } public void setRefund(int refund){ this.refund = refund; } public void refund() throws Exception{ if(refund!=4){ throw new Exception("狗狗的腿是四肢"); } } @Override public void call() { System.out.println("汪汪。。。"); } }
程序代码:
public class Dolphins extends Animal{ public Dolphins(){ } public Dolphins(String name){ super(name); } public void call() { System.out.println("海豚之音。。。"); } }
程序代码:
public class Duck extends Animal implements Refund { private int refund; public Duck(){ } public Duck(String name,int refund){ super(name); this.refund=refund; } public int getRefund() { return refund; } public void setRefund(int refund){ this.refund = refund; } @Override public void refund() throws Exception { // TODO Auto-generated method stub try{ if(refund!=2){ throw new Exception("鸭子的腿是两肢"); } }catch(Exception e){ e.printStackTrace(); } } @Override public void call() { System.out.println("嘎嘎。。。"); } }
程序代码:
public interface Refund { void refund() throws Exception; }
程序代码:
import java.util.InputMismatchException; import java.util.Scanner; public class Text { private static Scanner input; public static void main(String[] args) { input = new Scanner(System.in); int choose = 0; do { System.out.println("请选择动物"); System.out.println("1.狗狗"); System.out.println("2.鸭子"); System.out.println("3.海豚"); System.out.println("4.退出"); System.out.println("请输入你的选择:"); choose = input.nextInt(); switch (choose) { case 1: dog(); break; case 2: duck(); break; case 3: dolphins(); break; case 4: System.exit(0); } System.out.println("-------------"); System.out.println("输入任意数返回:"); choose = input.nextInt(); } while (choose != 241349112); } public static void dog() { int refund = 0; boolean flag = false; do { try { Dog a = new Dog(); // System.out.println(((Dog) a).getName()); System.out.println("输入狗狗的名字:"); String name = input.next(); ((Dog) a).setName(name); // System.out.println(((Dog) a).getName()); System.out.println("请输入狗狗的腿的个数:"); try{ refund = input.nextInt(); // input.hasNextInt(refund); }catch(InputMismatchException i){ flag=true; // i.printStackTrace(); } ((Dog) a).setRefund(refund); ((Dog) a).refund(); // System.out.println(((Dog) a).getRefund()); System.out.println("这个狗狗名字是:" + ((Dog) a).getName() + ",有" + ((Dog) a).getRefund() + "条腿"); a.call(); flag = false; } catch (Exception e) { System.out.println(e); flag = true; } } while (flag); } public static void duck() { int refund = 0; boolean flag = false; do { try { Animal a = new Duck(); System.out.println("输入鸭子的名字:"); String name = input.next(); ((Duck) a).setName(name); System.out.println("请输入鸭子的腿的个数:"); refund = input.nextInt(); ((Duck) a).setRefund(refund); ((Duck) a).refund(); System.out.println("这个鸭子名字是:" + ((Duck) a).getName() + ",有" + ((Duck) a).getRefund() + "条腿"); a.call(); flag = false; } catch (Exception e) { System.out.println(e); flag = true; } } while (flag); } public static void dolphins() { boolean flag = false; do { try { Animal a = new Dolphins(); System.out.println("输入海豚的名字:"); String name = input.next(); ((Dolphins) a).setName(name); System.out.println("这个海豚名字是:" + ((Dolphins) a).getName() + ",海豚没有腿的"); a.call(); flag = false; } catch (Exception e) { System.out.println(e); flag = true; } } while (flag); } }