Java 如何解决'隐式超级构造函数 classA() 不可见。必须显式调用另一个构造函数'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3904355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:35:43  来源:igfitidea点击:

How to resolve 'Implicit super constructor classA() is not visible. Must explicitly invoke another constructor'?

javaconstructor

提问by Rakesh Juyal

I am having a class 'ClassA' which is having private constructor.

我有一个具有私有构造函数的类“ClassA”。

public final class ClassA{
  private ClassA{
  }

  public static void main(String[] arg) }{
  ;
  ;
  ;
  }
}

Now, i am extending the class 'ClassA' [ finalkeyword is removed before doing this ]

现在,我正在扩展类“ClassA”[在执行此操作之前删除了final关键字]

public class ClassB extends ClassA{
     public static void main(String[] arg) }{
      ;
      ;
      ;
      }

}

Now, i am getting Implicit super constructor classA() is not visible. Must explicitly invoke another constructor. What does it mean and how to resolve this?

现在,我得到了Implicit super constructor classA() is not visible. Must explicitly invoke another constructor。这是什么意思以及如何解决这个问题?

Notei can not change the access specifier of ClassA constructor.

请注意,我无法更改 ClassA 构造函数的访问说明符。

采纳答案by Julien Hoarau

Change the constructor visibility of ClassA from privateto protected.

将 ClassA 的构造函数可见性从 更改privateprotected

Constructors always begin by calling a superclass constructor. If the constructor explicitly contains a call to a superclass constructor, that constructor is used. Otherwise the parameterless constructor is implied. If the no-argument constructor does not exist or is not visible to the subclass, you get a compile-time error.

构造函数总是以调用超类构造函数开始。如果构造函数显式包含对超类构造函数的调用,则使用该构造函数。否则隐含无参数构造函数。如果无参数构造函数不存在或对子类不可见,则会出现编译时错误。

回答by Nikita Rybak

Changing private ClassA{}to protected ClassA{}sounds like a good solution.

更改private ClassA{}protected ClassA{}听起来是一个不错的解决方案。

Parent constructor is always called in child class: implicitly or not. So, your ClassBdefinition is equivalent to

父构造函数总是在子类中调用:隐式或不隐式。所以,你的ClassB定义等价于

public ClassB extends ClassA {
    public ClassB() {
        super();
    }

    // all other methods you have go here...
}

If the only constructor of ClassAis private, it can't be called from ClassB.

如果 的唯一构造函数ClassA是私有的,则不能从 调用它ClassB

回答by Giorgos Dimtsas

Java will implicitly create a constructor with no parameters for ClassB, which will call super(). In your case the constructor in ClassA is not visible, hence the error you are getting. Changing the visibility to public or protected will resolve the error.

Java 将隐式地为 ClassB 创建一个没有参数的构造函数,它将调用 super()。在您的情况下, ClassA 中的构造函数不可见,因此会出现错误。将可见性更改为 public 或 protected 将解决错误。

回答by Bivas

I would suggest composition instead of inheritance (maybe that's what the designer of ClassAintended for class usage. Example:

我会建议组合而不是继承(也许这就是设计者ClassA用于类使用的目的。例如:

public class ClassB {
   private ClassA classA;

   ClassB() {
       // init classA
       ...
   }

   public ClassA asClassA() {
       return classA;
   }

   // other methods and members for ClassB extension
}

You can delegate methods from ClassBto ClassAor override them.

您可以从ClassBto委托方法ClassA或覆盖它们。