//由于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()); }
}
解决方案 »
- [笔试题]线程资源竞争而死锁
- java手机应用
- 求AWT或SWING电子版的入门书籍
- 比较小时问题
- 麻烦大家帮我看看这个程序,感谢了!
- 新手上路,请各位多多关照
- 跪求sybase或oracle数据库中怎么才能实时获得某个表新插入的数据
- 如何在weblogic8.1下改变web容器的部署描述符文件名称
- 请问怎么编译才可以使applet在微软的虚拟机里运行啊。就是不需要下载SUN的运行时?
- 在win2000下,怎么运行图标是记事本的*.java程序?(特急)
- 字符串转换:+-*/()转为add,subtract,multiply,divide
- 调查:大家对对象容器学习的总结,谢谢,奉上全部分数100分
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);
}}
age用int就行了
加get set方法
main方法
public static void main(String[] args){
Animal cat = new Cat();
cat.setHeight(1.0);
System.out.println(cat.getHeight);
}
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;
}
}
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的 直接使用类名调用
……
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);