Can not resolve symbol, abstract class factory
我是JavaNeXT的新手,我将遵循测试驱动开发的例子。在第8章中,我们将法郎和美元的子类转化为货币。我已经为franc和dollar创建了类似的工厂方法,但是当我尝试运行测试时,我得到了测试行的"无法解析符号dollar"(或franc):
1 2 | Money five= new Money.dollar(5); Money four= new Money.franc(5); |
我已经从头开始了两次,试着用谷歌搜索,但我不确定我遗漏了什么。
货币类别:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public abstract class Money { abstract Money times(int multiplier); protected int amount; static Money dollar(int amount){ return new Dollar(amount); } static Money franc(int amount){ return new Franc(amount); } public boolean equals(Object object){ Money money = (Money) object; return amount == money.amount && getClass().equals(money.getClass()); } } |
法郎(美元相同):
1 2 3 4 5 6 7 8 9 10 11 12 | public class Franc extends Money { Franc(int amount){ this.amount = amount; } Money times(int multiplier){ return new Franc(amount * multiplier); } } |
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import org.junit.Test; import static org.junit.Assert.*; public class MoneyTest { @org.junit.Test public void testMultplication(){ Money five= new Money.dollar(5); assertEquals(new Dollar(10), five.times(2)); assertEquals(new Dollar(15), five.times(3)); Money four= new Money.franc(5); assertEquals(new Franc(10), four.times(2)); assertEquals(new Franc(15), four.times(3)); } } |
1 | Money five= new Money.dollar(5); |
是错误的语法。把它改成:
1 | Money five= Money.dollar(5); |
因为
您使用的是工厂模式;根本不需要使用
相反,使用工厂:
1 2 | Money five= Money.dollar(5); Money four= Money.franc(5); |
我也鼓励对你的目标有专一性;一个是