如何访问类的私有构造函数?

编程入门 行业动态 更新时间:2024-10-28 16:20:57
本文介绍了如何访问类的私有构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一名Java开发人员。在一次采访中,我被问到私人构造函数的问题:

你能访问一个类的私有构造函数并实例化它吗? >

我回答不,但是错了。

解决方案

  • 您可以访问它
  • 如果是嵌套类,则可以从封闭类中访问它
  • 这不是真的清楚,如果这些都适用 - 你能提供更多的信息吗?

    I am a Java developer. In an interview I was asked a question about private constructors:

    Can you access a private constructor of a class and instantiate it?

    I answered 'No' but was wrong.

    Can you explain why I was wrong and give an example of instantiating an object with a private constructor?

    解决方案

    • You can access it within the class itself (e.g. in a public static factory method)
    • If it's a nested class, you can access it from the enclosing class
    • Subject to appropriate permissions, you can access it with reflection

    It's not really clear if any of these apply though - can you give more information?

更多推荐

如何访问类的私有构造函数?

本文发布于:2023-10-31 07:02:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!