//由于Animal
abstract class Animal {
protected String color;
protected double age;
protected String name;
}class Fish extends Animal {
private double speed; public double getSpeed() {
return speed;
} public void setSpeed(double speed) {
this.speed = speed;
}
}class Cat extends Animal {
private double height; public double getHeight() {
return height;
} public void setHeight(double height) {
this.height = height;
}
}public class TestAnimal { public static void main(String args[]) {
Fish f = new Fish();
f.setSpeed(456) ;
System.out.println(f.getSpeed()); }
}

解决方案 »

  1.   


    import java.util.Scanner;public class Test9 { public static void main(String args[]) { Scanner s = new Scanner(System.in);
    try {
    print(Integer.parseInt(s.next()));
    } catch (NumberFormatException e) {
    System.out.println("输入的不是数字");
    } } public static void print(int s) {
    int count = 1;
    for (int i = 1; i <= s; i++) {
    count *= i;
    } System.out.println(count);
    }}
      

  2.   

    首先第一个animal应该是public abstract class Animal
    age用int就行了
    加get set方法
    main方法
    public static void main(String[] args){
          Animal cat = new Cat();
          cat.setHeight(1.0);
          System.out.println(cat.getHeight);
    }
      

  3.   

    import java.awt.Color;
    public class Test {
    public static void main(String[] args) {
    Cat tom=new Cat("JunGe",Color.YELLOW,99,100);
    Fish lili=new Fish("CaiRong",Color.BLACK,1,1);
    System.out.println(tom);
    System.out.println(lili);
    }
    }class Animal {
     String name;
     Color color;
     int age;
    public Animal(String name,Color color,int age) {
    this.name=name;
    this.color=color;
    this.age=age;
    }
    public String toString() {
    return "名字"+this.name+" 颜色"+this.color+"年龄"+this.age;
    }}class Cat extends Animal {
    int height;
    public Cat(String name,Color color,int age,int height) {
    super(name,color,age);
    this.height=height;
    }
    public String toString() {
    return super.toString()+"高度"+this.height;
    }}class Fish extends Animal {
    int speed;
    public Fish(String name,Color color,int age,int speed){
    super(name,color,age);
    this.speed=speed;
    }
    public String toString() {
    return super.toString()+"速度"+this.speed;
    }
    }
      

  4.   


    public static int JC(int x) {
     int temp=1;//初值设为1。
     for(int i=1;i<=x;i++) {
     temp*=i;
     }
     return temp;
     }用时用System.out.println(Test9.JC(x))
    JC函数将参数x值传入 返回x的阶乘 JC定义为static的 直接使用类名调用 
      

  5.   

    calloner1 的基础上 再建一个工厂类。通过传进的参数生成不同的动物。然后在new cat和new Fish 的地方就可以直接new 工厂类,并指定生成的是cat 还是fish 就ok。(不过要先在 Animal 类中再添加otherParam参数) 
    ……
    public class AnimalFactory{
       //create的方法中通过String告诉工厂类的create方法,需要生成cat或fish,然后接下来的其他参数
       //为cat或fish的属性。其中color是以字符串的方式输出的。
       public Animal create(String animal,String name,String color,int age,
       int otherParam){
       if("cat".equals(animal)){
       Animal animal=new Cat(name,color,age,otherParam);
       }else if("fish".equals(animal)){
       Animal animal=new Fish(name,color,age,otherParam);}
       return  animal;
    }

    //使用工厂类AnimalFactory的方法:
    ……
    AnimalFactory af=new AnimalFactory();
    //生成cat:
    Animal cat=af.create("cat","tom",yellow,3,99);
    Animal fish=af.create("fish","beauty",red,7,39);
    System.out.println(tom); 
    System.out.println(beauty); 
      

  6.   

    不好意思,字符串 yellow 和 red 要用引号包含...