package com.bdqn.dome6;
public class Class {
package com.project.demo;
import java.util.Scanner;
import org.apache.log4j.Logger;
/**
* 动物父类
* @author suixin
*
*/
public abstract class Animal {
private static Logger logger = Logger.getLogger(Animal.class.getName());
private String name = ""; //昵称
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Animal() {
super();
// TODO Auto-generated constructor stub
}
public Animal(String name) {
super();
this.name = name;
}
public abstract void shout() ; //动物叫
}
package com.project.demo;
/**
* 蛇类
* @author suixin
*
*/
public class Cat extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("嘶嘶斯......");
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String name,int letNum ) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 胖子类
* @author suixin
*
*/
public class Dolphin extends Animal {
public Dolphin() {
super();
// TODO Auto-generated constructor stub
}
public Dolphin(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("亚麻跌......");
}
}
package com.project.demo;
/**
* 鸭类
* @author suixin
*
*/
public class Duck extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
public Duck() {
super();
// TODO Auto-generated constructor stub
}
public Duck(String name,int letNum) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("板鸭板鸭板鸭......");
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 获取腿的数量的接口
* @author suixin
*
*/
public interface Terrestrial {
int getLegNum(); //腿的数量
}
package com.project.demo;
import java.util.Scanner;
/**
* 测试类
* @author suixin
*
*/
public class Test {
//创建动物类对象数组
static Animal animal [] = new Animal[3];
static Scanner scanner = new Scanner(System.in);
static int choose;
public static void main(String[] args) {
start();
while (choose != 0) {
try {
modifyAnimal();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
show();
choose = scanner.nextInt();
}
}
}
/**
* 打印信息方法
*/
public static void show() {
System.out.println("动物名字\t\t腿的条数\t\t动物叫");
for (int i = 0; i < animal.length; i++) {
if (animal[i] != null) {
if (animal[i] instanceof Terrestrial) {
String name = animal[i].getName();
int legNum = ((Terrestrial) animal[i]).getLegNum();
System.out.print(name + "\t\t" + legNum + "\t\t");
animal[i].shout();
}else {
String name = animal[i].getName();
System.out.print(name + "\t\t" + 0 + "\t\t");
animal[i].shout();
}
}
}
System.out.println("是否要继续修改数据:按0进行修改操作,其它任意数字键退出:");
}
/**
* 给对象数组初始化
*/
public static void start() {
animal[0] = new Cat("九头日天蛇", 4);
animal[1] = new Duck("飞天天鸭", 2);
animal[2] = new Dolphin("大胖丸子");
show();
choose = scanner.nextInt();
}
/**
* 更改信息方法
* @throws Exception
*/
public static void modifyAnimal() throws Exception{
//定义变量存储临时数据
String [] names = new String[3];
int [] number = new int [3];
System.out.print("请输入蛇的名称:");
names[0] = scanner.next();
System.out.println("请输入蛇腿的条数");
number[0] = scanner.nextInt();
if (number[0] != 4) {
throw new Exception("蛇只能有1000条腿");
}
System.out.println("请输入天鸭的名称");
names[1] = scanner.next();
System.out.println("请输入天鸭腿的条数");
number[1] = scanner.nextInt();
if (number[1] != 2) {
throw new Exception("天鸭只能有3条腿");
}
System.out.println("大胖丸子");
names[2] = scanner.next();
animal[0] = new Cat(names[0], number[0]);
animal[1] = new Duck(names[1], number[1]);
animal[2] = new Dolphin(names[2]);
}
}
}
public class Class {
package com.project.demo;
import java.util.Scanner;
import org.apache.log4j.Logger;
/**
* 动物父类
* @author suixin
*
*/
public abstract class Animal {
private static Logger logger = Logger.getLogger(Animal.class.getName());
private String name = ""; //昵称
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Animal() {
super();
// TODO Auto-generated constructor stub
}
public Animal(String name) {
super();
this.name = name;
}
public abstract void shout() ; //动物叫
}
package com.project.demo;
/**
* 蛇类
* @author suixin
*
*/
public class Cat extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("嘶嘶斯......");
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String name,int letNum ) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 胖子类
* @author suixin
*
*/
public class Dolphin extends Animal {
public Dolphin() {
super();
// TODO Auto-generated constructor stub
}
public Dolphin(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("亚麻跌......");
}
}
package com.project.demo;
/**
* 鸭类
* @author suixin
*
*/
public class Duck extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
public Duck() {
super();
// TODO Auto-generated constructor stub
}
public Duck(String name,int letNum) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("板鸭板鸭板鸭......");
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 获取腿的数量的接口
* @author suixin
*
*/
public interface Terrestrial {
int getLegNum(); //腿的数量
}
package com.project.demo;
import java.util.Scanner;
/**
* 测试类
* @author suixin
*
*/
public class Test {
//创建动物类对象数组
static Animal animal [] = new Animal[3];
static Scanner scanner = new Scanner(System.in);
static int choose;
public static void main(String[] args) {
start();
while (choose != 0) {
try {
modifyAnimal();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
show();
choose = scanner.nextInt();
}
}
}
/**
* 打印信息方法
*/
public static void show() {
System.out.println("动物名字\t\t腿的条数\t\t动物叫");
for (int i = 0; i < animal.length; i++) {
if (animal[i] != null) {
if (animal[i] instanceof Terrestrial) {
String name = animal[i].getName();
int legNum = ((Terrestrial) animal[i]).getLegNum();
System.out.print(name + "\t\t" + legNum + "\t\t");
animal[i].shout();
}else {
String name = animal[i].getName();
System.out.print(name + "\t\t" + 0 + "\t\t");
animal[i].shout();
}
}
}
System.out.println("是否要继续修改数据:按0进行修改操作,其它任意数字键退出:");
}
/**
* 给对象数组初始化
*/
public static void start() {
animal[0] = new Cat("九头日天蛇", 4);
animal[1] = new Duck("飞天天鸭", 2);
animal[2] = new Dolphin("大胖丸子");
show();
choose = scanner.nextInt();
}
/**
* 更改信息方法
* @throws Exception
*/
public static void modifyAnimal() throws Exception{
//定义变量存储临时数据
String [] names = new String[3];
int [] number = new int [3];
System.out.print("请输入蛇的名称:");
names[0] = scanner.next();
System.out.println("请输入蛇腿的条数");
number[0] = scanner.nextInt();
if (number[0] != 4) {
throw new Exception("蛇只能有1000条腿");
}
System.out.println("请输入天鸭的名称");
names[1] = scanner.next();
System.out.println("请输入天鸭腿的条数");
number[1] = scanner.nextInt();
if (number[1] != 2) {
throw new Exception("天鸭只能有3条腿");
}
System.out.println("大胖丸子");
names[2] = scanner.next();
animal[0] = new Cat(names[0], number[0]);
animal[1] = new Duck(names[1], number[1]);
animal[2] = new Dolphin(names[2]);
}
}
}