如何防止在 Java 中使用默认构造函数?
在我的评估中,它说:
"We don't want the user to use the default constructor since the user has to specify the HashCode, and maximum load factor"
我认为这可以解决问题,但显然不是(字典是一个用于抛出异常的类):
public boolean HashDictionary() throws DictionaryException {}
字典异常类:
public class DictionaryException extends Throwable {
}
测试以确保它在使用默认构造函数时抛出异常(由讲师提供):
try
{
HashDictionary h = new HashDictionary();
System.out.println("***Test 1 failed");
}
catch (DictionaryException e) {
System.out.println(" Test 1 succeeded");
}
我只想知道如何做到这一点,因为我不熟悉这样做的方法。谢谢。