日期:2014-05-20  浏览次数:20759 次

工厂模式 哪儿有误?
public interface Dice{
    int throw();
}

public interface FlipCoin{
    int throw();
}

public interface DiceFactory{
    Dice getDice();
}

public interface FlipCoinFactory{
    FlipCoin getFlipCoin();
}

import java.util.*;
public class Decide implements FlipCoin{
    private static Random rand = new Random(47);
    
    public int throw(){
return rand.nextInt(2);
    }
}

import java.util.*;
public class Gamble implements Dice{
    private static Random rand = new Random(47);
    
    public int throw(){
return rand.nextInt(6);
    }
}

public class DecideFactory implements FlipCoinFactory{
    FlipCoin getFlipCoin(){return new Decide();}
}

public class GambleFactory implements DiceFactory{
    Dice getDice(){return new Gamble();}
}

public class Happy{
    public static void playDice(DiceFactory df){
Dice d = df.getDice();
System.out.println(d.throw());
    }

    public static void playFlipCoin(FlipCoinFactory fcf){
FlipCoin fc = fcf.getFlipCoin();
if(fc.throw() == 1){System.out.println("Positive.");}
else {System.out.println("Negtive.");}
    }

    public static void main(String[] args){
playDice(new GambleFactory());
playFlipCoin(new DecedeFactory());
    }
}

编译通不过,不知道这个throw方法有什么问题??????
------解决方案--------------------
throw是个关键字,换个吧;