package com.bdqn.dongwu;
public abstract class Animal {
public Animal(String name) {
super();
this.name = name;
}
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void print();
}
package com.bdqn.dongwu;
public class Cat extends Animal implements Terrestrial{
public Cat(String name,int sNum) {
super(name);
this.sNum = sNum;
// TODO Auto-generated constructor stub
}
int sNum=0;
public void setsNum(int snum) {
sNum = snum;
}
public int getsNum() {
return sNum;
}
@Override
public int getNum() {
// TODO Auto-generated method stub
System.out.println("猫有"+this.getsNum()+"腿");
return sNum;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("m m");
}
}
package com.bdqn.dongwu;
public class Dolphin extends Animal {
public Dolphin(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("ww");
}
}
package com.bdqn.dongwu;
public class Duck extends Animal implements Terrestrial{
public Duck(String name,int sNum) {
super(name);
this.sNum = sNum;
// TODO Auto-generated constructor stub
}
int sNum;
public int getsNum() {
return sNum;
}
public void setsNum(int sNum) {
this.sNum = sNum;
}
@Override
public int getNum() {
// TODO Auto-generated method stub
//System.out.println("鸭有"+this.getsNum()+"腿");
return sNum;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("gaga");
}
}
package com.bdqn.dongwu;
public interface Terrestrial {
int getNum();
}
package com.bdqn.dongwu;
import java.util.Scanner;
public class Test {
static Animal arr[] = new Animal[3];
static Scanner input = new Scanner(System.in);
static int choise;
public static void start() {
// TODO Auto-generated method stub
arr[0] = new Cat("加菲猫",4);
arr[1] = new Duck("唐小鸭",2);
arr[2] = new Dolphin("大海吨");
load();
choise = input.nextInt();
}
public static void main(String[] args) {
start();
while(choise == 0) {
try {
modify();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
load();
choise = input.nextInt();
}
}
System.out.println("谢谢使用");
}
public static void load() {
System.out.println("动物名字\t\t腿的条数\t\t动物叫声");
for(int i=0; i< arr.length;i++) {
if(arr[i] instanceof Terrestrial) {
String name = arr[i].getName();
int sNum = ((Terrestrial) arr[i]).getNum();
System.out.println(name+"\t\t" + sNum+"\t\t");
}else {
String name = arr[i].getName();
System.out.println(name+"\t\t"+0+"\t\t");
arr[i].print();
}
}
System.out.println("是否要进行修改数据:按0进行修改操作,其他任意数字退出");
}
public static void modify() throws Exception {
String[] names = new String[3];
int[] number = new int[3];
System.out.println("请输入猫的名称");
names[0] = input.next();
System.out.println("请输入猫腿的条数");
number[0] = input.nextInt();
if (number[0] != 4) {
throw new Exception("猫只能有4条腿");
}
System.out.println("请输入鸭子的名称");
names[1] = input.next();
System.out.println("请输入鸭子腿的条数");
number[1] = input.nextInt();
if (number[1] != 2) {
throw new Exception("鸭子只能有两条腿");
}
System.out.println("请输入海豚的名称");
names[2] = input.next();
arr[0] = new Cat(names[0], number[0]);
arr[1] = new Duck(names[1], number[1]);
arr[2] = new Dolphin(names[2]);
}
}
动物乐园
最新推荐文章于 2021-02-13 14:28:43 发布