“构造函数不可见”错误 [英] "The constructor is not visible" error

查看:180
本文介绍了“构造函数不可见”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:

首先,使用一个构造函数:

First, with one constructor :

public class First {

    First (ObjectA myObjectA) {
        //do stuff
    }
}

其次,使用两个构造函数:

And Second, with two constructors :

public class Second {

    Second (ObjectB myObjectB) {
        //do something...
    }

    Second (ObjectC myObjectC) {
        //do something else...
    }
}

想要实例化我的第一类,Eclipse生成一个错误(构造函数不可见),我必须添加 public 的构造函数

When I want to instantiate my First class, Eclipse generates me an error ("The constructor is not visible"), I have to add public to the constructor of First :

First first = new First(myObject); //Error : "The constructor is not visble"

但是当我实例化我的第二类,我没有错误:

But when I instantiate my Second class, I have no error :

Second second = new Second(myObjectC); //No error...

注意:我实例化 c $ c>在另一个类,但我实例化第二第一第一个第二个在同一个包中。

Notes: I instantiate First in an other class, but I instantiate Second in First. First and Second are in the same package.

推荐答案

您的构造函数的无访问修饰符使其成为包私有
假设First和Second在同一个包中,你可以从第一个调用Second的构造函数。
另一个类从另一个包,但是,不能访问任何构造函数。

No access modifier for your constructor makes it package private. Assuming that First and Second are in the same package, you can call Second's constructors from first. Another class from another package, however, cannot access any of the constructors.

这篇关于“构造函数不可见”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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