简体   繁体   English

默认构造函数java的抛出异常

[英]Throw exception for default constructor java

How do I prevent the default constructor from being used in Java? 如何防止在Java中使用默认构造函数?

In my assessment it says: 在我的评估中说:

"We don't want the user to use the default constructor since the user has to specify the HashCode, and maximum load factor"

I thought this would do the trick, but apparently not (dictionary is a class that is used to throw exceptions): 我以为这可以解决问题,但显然不行(字典是用于引发异常的类):

public boolean HashDictionary() throws DictionaryException {}

DictionaryException Class: DictionaryException类:

public class DictionaryException extends Throwable {

}

Test to make sure it throws an exception when using default contructor(Supplied by lecturer): 测试以确保在使用默认构造函数(由讲师提供)时引发异常:

try
{
    HashDictionary h = new HashDictionary();
    System.out.println("***Test 1 failed");

}
catch (DictionaryException e) {
        System.out.println("   Test 1 succeeded");
}

I just want to know how I could do this, as I'm not familiar with a method of doing it. 我只是想知道我该怎么做,因为我不熟悉这样做的方法。 Thanks. 谢谢。

You can declare the default one as private if you do not want it to be called. 如果不想调用默认值,则可以将其声明为私有。

To answer your comment, you can throw an exception- 要回答您的评论,您可以抛出一个例外-

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor is not allowed.");
}

You can 您可以

a) Omit the default constructor a)省略默认构造函数

b) Make the default constructor private b)将默认构造函数设为私有

c) Throw an exception when the default constructor is used c)使用默认构造函数时引发异常

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor should not be used!");
}

Don't do it like that. 不要那样做。 Just make the default constructor private: 只需将默认构造函数设为私有:

private HashDictionary() {}

EDIT: What was the boolean doing in the Constructor definition? 编辑:构造函数定义中的boolean做什么的? I just copied it... 我只是复制了...

If you don't want the user to use the default constructor you can either omit the constructor if other non-default constructors are declared, or you can make the default constructor private . 如果您不希望用户使用默认构造函数,则可以在声明了其他非默认构造函数的情况下省略该构造函数,也可以将默认构造函数设为private

The other issue you may be having is that the declaration you put in your question is not a constructor, it is a method. 您可能遇到的另一个问题是,您在问题中放入的声明不是构造函数,而是方法。 Constructors do not have return types. 构造函数没有返回类型。

Change this 改变这个

public boolean HashDictionary() throws DictionaryException {}

to this 对此

public HashDictionary() throws DictionaryException {}

better yet just change it to 更好的只是将其更改为

private HashDictionary() {}

and then no one can access it outside of the class itself. 然后没有人可以在课程本身之外访问它。

I ended up adding this: 我最终添加了这个:

 public DictionaryException(String error_string)
 {
     System.out.println(error_string);
 }

as the default constructor for DictionaryException 作为DictionaryException的默认构造函数

And then: 接着:

public HashDictionary() throws DictionaryException {
    throw new DictionaryException("Default constructor should not be used!");
}

This for the default constructor of the HashDictionary, the issue was that the type was a boolean, so I removed that and it seemed to work. 对于HashDictionary的默认构造函数,问题是该类型是布尔值,因此我删除了它,并且似乎可以正常工作。

  1. To Forbid the Default Constructor To Be Called from Outside the Class, use: 要禁止从类外部调用默认构造函数,请使用:

    private HashDictionary(){} 私人HashDictionary(){}

  2. To Forbid the Default Constructor To Be Called from Even Inside the Class, use: 若要禁止甚至从类内部调用默认构造函数,请使用:

    Declare a parameterized constructor and don't write the default non-parameterized constructor inside the class. 声明一个参数化的构造函数,不要在类内部编写默认的非参数化构造函数。 public HashDictionary(String string){ } 公共HashDictionary(字符串字符串){}

  3. If you want some exception to be thrown while calling out the constructor, then: 如果希望在调用构造函数时抛出一些异常,则:

    public HashDictionary() throws Exception { 公共HashDictionary()引发异常{
    throw new Exception("throwing exception from constructor"); 抛出新的异常(“从构造函数中抛出异常”);
    } }

Regards, Birendra. 问候,比伦德拉。

If you don't want to use a default constructor but you want to instantiate a class you need to implement a custom constructor. 如果您不想使用默认构造函数,但是想实例化一个类,则需要实现一个自定义构造函数。 You have to tell the user what is needed to create a valid instance. 您必须告诉用户创建有效实例所需的内容。 And that's it! 就是这样! Default constructor won't be available anymore. 默认构造函数将不再可用。 If a constructor logic is too complicated, feel free to extract private methos, including a private constructor if you feal it's ok in your case. 如果构造函数的逻辑过于复杂,请随意提取私有方法,包括私有构造函数(如果您认为合适的话)。 If you have many ways of creating instance you can use static factory-like methods and private ctor. 如果您有多种创建实例的方法,则可以使用类似于工厂的静态方法和私有ctor。 Making a private ctor only to disable the default one makes no sense (at least I don't see one). 仅将私有ctor设置为禁用默认值是没有意义的(至少我看不到)。

If you don't want to instantiate a class (I mean you dont want to use the default ctor and you have no reason to create a custom ctor) this means you want to use only a static fields/methods, so make whole class static. 如果您不想实例化一个类(我的意思是您不想使用默认的ctor,并且没有理由创建自定义ctor),这意味着您只想使用静态字段/方法,因此请将整个类设为静态。

Oh, I assume that we're talking about non abstract classes. 哦,我假设我们在谈论非抽象类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM