当前位置: 代码迷 >> Java相关 >> 小弟第二回发帖,跪求个大神指点一二啊 啊
  详细解决方案

小弟第二回发帖,跪求个大神指点一二啊 啊

热度:741   发布时间:2016-04-22 21:09:46.0
小弟第二次发帖,跪求个大神指点一二啊啊啊 啊.
这是小弟第一次发帖的地址,望大神有时间去看看.  指点指点,感激不尽
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() {
  相关解决方案