【发布时间】:2013-10-21 14:30:54
【问题描述】:
如何防止在 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 {}
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");
}
我只是想知道如何做到这一点,因为我不熟悉这样做的方法。谢谢。
【问题讨论】:
标签: java class oop constructor