简体   繁体   English

Java中的父/子构造函数

[英]Parent/Child Constructor in java

I keep getting this error required:boolean; 我不断收到此错误要求:布尔值; found:no arguments; 找不到:没有论据; reason:actual and formal arguments lists differ in length; 原因:实际和形式论证清单的长度不同;

I know why this is, because my constructor in my superclass and subclass don't match...but i dont want to make changes to them if possible because it'll ruin the rest of my other classes and if i do need to make changes i'd rather just make changes to the LowRights class. 我知道为什么会这样,因为我的超类和子类中的构造函数不匹配...但是如果可能的话,我不想对其进行更改,因为这会破坏其余的其他类,并且如果我确实需要更改我只想更改LowRights类。 Could someone point me in the right direction? 有人可以指出我正确的方向吗?

public class LowRights extends SecurityRights
{
    private String name;

    public LowRights(String n){
        this.name = n;
        boolean right = getRight(); // Added
        setRight(false); // Added
    }

    public boolean setRight(boolean right){
        return right;
    }

    public String getName(){
        return name;
    }

    public static void main(String[] a){
    LowRights s= new LowRights("Lisa");
    System.out.print(s.getName() +" "+s.getSecret());                
  }

}

This is my super class: 这是我的超级班:

public class SecurityRights
{
private boolean right;
private boolean canreadSecret;
String SECRET="the secret is 42";



public SecurityRights(boolean r)
{
 r = right;
 if (r) canreadSecret=true; else canreadSecret=false;
}

boolean getRight(){
 return right;
}

boolean canReadSecret(){
 return canreadSecret;
}

String getSecret(){
 if (canreadSecret) return SECRET; else return "access denied";
}

}

You need for sure make one change. 您肯定需要进行一次更改。 Easier will be to add as the first instruction in your child class this: 较容易将以下内容添加为您的孩子课程的第一条指令:

super(false);

or: 要么:

super(true);

depending in what makes sense for you. 取决于对您有意义的。

By the way, in your parent's constructor you have this: 顺便说一句,在您父母的构造函数中,您需要:

r = right;

but it should be: 但应该是:

right = r;

Otherwise, you are discarding the value passed in the constructor. 否则,您将丢弃在构造函数中传递的值。

In a child-class constructor, a call to a parent-class constructor is always the first line. 在子类构造函数中,对父类构造函数的调用始终是第一行。 Even if you don't write it, it's there. 即使您不写,它也在那里。 So your code actually looks like this: 因此,您的代码实际上如下所示:

public LowRights(String n){
        super(); //implied no-args parent-class constructor
        this.name = n;
        boolean right = getRight(); // Added
        setRight(false); // Added
}

The problem is that your parent-class doesn't have a no-args constructor. 问题在于您的父类没有no-args构造函数。 You have to give it a boolean. 您必须给它一个布尔值。 How you determine that boolean is up to you. 如何确定布尔值取决于您。 This would pass in false: 这将传递错误:

public LowRights(String n){
        super(false); //calls the parent-class constructor
        this.name = n;
        boolean right = getRight(); // Added
        setRight(false); // Added
}

This is just a generic example, because a lot of your code doesn't make sense. 这只是一个通用示例,因为很多代码没有意义。 Your parent-class constructor doesn't actually set the value of the right boolean, for example. 例如,您的父类构造函数实际上并未设置正确的布尔值。

According to documentation : 根据文件

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. 如果构造函数未显式调用超类构造函数,则Java编译器会自动将调用插入到超类的无参数构造函数中。 If the super class does not have a no-argument constructor, you will get a compile-time error. 如果超类没有无参数构造函数,则将出现编译时错误。 Object does have such a constructor, so if Object is the only superclass, there is no problem. 对象确实具有这样的构造函数,因此如果对象是唯一的超类,就没有问题。

The less impact way would be defining a default value to right attribute on super class: 影响较小的方法是在超类上为right属性定义一个默认值:

public SecurityRights() {
    this(true);
}

LowRights needs to call super(right); LowRights需要调用super(right); or SecurityRights needs to add another constructor. SecurityRights需要添加另一个构造函数。

Child classes need to call thier parent constructors. 子类需要调用其父构造函数。

public LowRights(String n) {
    super(false) 
    ... <your sub class constructor code>
}

The error occurs, because the constructor LowRights(String n) calls the default constructor of SecurityRights (Java adds super() as the first line of the constructor if it is not specified what parent constructor to call). 发生错误是因为构造函数LowRights(String n)调用了SecurityRights的默认构造函数(如果未指定要调用的父构造函数,则Java将super()添加为构造函数的第一行)。 But there is no default constructor in SecurityRights. 但是SecurityRights中没有默认的构造函数。 Default constructor is a constructor without arguments: SecurityRights(). 默认构造函数是不带参数的构造函数:SecurityRights()。 So you can add the default constructor to the SecurityRights class or to specify what constructor has to be called in LowRights: super(true). 因此,您可以将默认构造函数添加到SecurityRights类中,或指定在LowRights中必须调用的构造函数:super(true)。

each class when is being created always calls constructor of its superclass, 每个类在创建时总是调用其超类的构造函数,
in your example 在你的例子中

 public LowRights(String n){
            this.name = n;
...}

you were actually calling default constructor super() which doesn't exist in your superclass 您实际上是在调用super()类中不存在的默认构造函数super()

so you need to add call to existing constructor, and as you want to set right to be false, add call super(false) then this line setRight(false); 因此,您需要添加对现有构造函数的调用,并且要将right设置为false,请添加调用super(false)然后添加此行setRight(false); is not needed anymore 不再需要

Java adds super() as first line of every constructor if no superclass constructor was added explicitly (every class in Java silently inherits from Object class and it has only one, no-arg constructor). 如果未显式添加任何超类构造函数,则Java将super()作为每个构造函数的第一行添加(Java中的每个类都静默地继承自Object类,并且只有一个无参数的构造函数)。

SecurityRights class does not offer no-arg constructor - thats why you're getting this error. SecurityRights类不提供no-arg构造函数-这就是为什么出现此错误的原因。

You can either add no-arg constructor to SecurityRights class or add superconstructor call with boolean argument to LowRights(String n) constructor. 您可以将no-arg构造函数添加到SecurityRights类,或者将具有布尔参数的superconstructor调用添加到LowRights(String n)构造函数。

public LowRights(String n) {
   super(true) 
   ...
}

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

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