这是小弟第一次发帖的地址,望大神有时间去看看. 指点指点,感激不尽
http://bbs.csdn.net/topics/390790377?page=1#post-397407492
------解决方案--------------------
package cn.com.microproposal.test;
import java.util.Random;
public class Test{
Random random = new Random();
public FatherShape CreateShape() {
int RandomNumber = random.nextInt(4);
FatherShape s = null ;
if(RandomNumber ==1) {
s = new SonRectangle();
}
else if(RandomNumber==2) {
s = new SonCircle();
}else
s = new SonTriangle();
return s;
}
public static void main(String[] arge){
Test ShapeDemo = new Test();
FatherShape[] FatherShapes = new FatherShape[20];
for(int i = 0;i < FatherShapes.length; i++) {
FatherShapes[i] = ShapeDemo.CreateShape();
System.out.println("这是第"+FatherShape.c.getCount()+"次调用");
FatherShapes[i].PictureDraw();
}
}
}
class FatherShape {
static Counter c = new Counter();
public void PictureDraw() {
System.out.println("draw a picture");
}
}
class SonRectangle extends FatherShape {
public void PictureDraw() {
System.out.println("draw a picture: 矩形");
}
}
class SonCircle extends FatherShape {
public void PictureDraw() {
System.out.println("draw a picture:圆形");
}
}
class SonTriangle extends FatherShape {
public void PictureDraw() {
System.out.println("draw a picture : 三角形");
}
}
class Counter {
public int i = 0;
public int getCount() {
return ++i;
}
}
------解决方案--------------------
public class JavaTest {
Random random = new Random();
public FatherShape CreateShape(Counter count) {
int RandomNumber = random.nextInt(4);
FatherShape s = null;
if (RandomNumber == 1) {
s = new SonRectangle(count);
} else if (RandomNumber == 2) {
s = new SonCircle(count);
} else{
s = new SonTriangle(count);
}
return s;
}
public static void main(String[] arge) {
Counter count = new Counter();
JavaTest ShapeDemo = new JavaTest();
FatherShape[] FatherShapes = new FatherShape[20];
for (int i = 0; i < FatherShapes.length; i++) {
FatherShapes[i] = ShapeDemo.CreateShape(count);
FatherShapes[i].pictureDraw();
}
}
}
class FatherShape {
protected Counter count;
public FatherShape(Counter count){
this.count = count;
}
public void pictureDraw() {
count.getCount();
System.out.println("这是第" + count.i + "次调用");
}
}
class SonRectangle extends FatherShape {
public SonRectangle(Counter count) {
super(count);
}
@Override
public void pictureDraw() {
super.pictureDraw();
System.out.println("draw a picture: 矩形");
}
}
class SonCircle extends FatherShape {
public SonCircle(Counter count) {
super(count);
}
@Override
public void pictureDraw() {
super.pictureDraw();
System.out.println("draw a picture:圆形");
}
}
class SonTriangle extends FatherShape {
public SonTriangle(Counter count) {
super(count);
}
@Override
public void pictureDraw() {