防止默认构造函数的用法 [英] Prevent usage of default constructor

查看:96
本文介绍了防止默认构造函数的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以防止默认的构造函数的用法?

Is there a way to prevent the usage of the default constructor?

所有我能想到的是抛出异常,但我想的东西,导致编译时错误。

All I can think of is throwing an exception, but I would like something that causes a compile time error.

推荐答案


  • 如果一切都在类是静态的,可考虑将其静态类。这样,你不会得到一个构造函数都没有。

  • 如果你想有一个参数的构造函数,但你不希望它是公开的,明确宣布并使其私人(或内部等)

  • 如果你不想参数的构造函数,但是的的希望与参数的构造函数,那么就宣告那些 - 将不会生成默认的构造函数你

  • If everything in the class is static, consider making it a static class. That way, you won't get a constructor at all.
  • If you want a parameterless constructor but you don't want it to be public, declare it explicitly and make it private (or internal etc)
  • If you don't want a parameterless constructor but do want constructors with parameters, then just declare those - the default constructor won't be generated for you
  • 我的认为的应该涵盖所有基地...

    I think that should cover all bases...

    这篇关于防止默认构造函数的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆