未定义的构造函数(java) [英] Undefined constructor (java)

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

问题描述

所以我有一个名为User的Java类,其中包含这样的构造函数:

so I have a java class called User that contains a constructor like this:

public class User extends Visitor {
    public User(String UName, String Ufname){
        setName(UName);
        setFname(Ufname);
    }
}

然后他的问题出现在我的另一个名为Admin的类中:

And then the he problems occur in my other class called Admin:

public class Admin extends User {  //error "Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor"

//public Admin() { } //tried to add empty constructor but error stays there

    //}


    public void manage_items() {            

        throw new UnsupportedOperationException();
    }

    public void manage_users() {   

        throw new UnsupportedOperationException();
    }
}

我收到错误Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor at the place marked in the code.,我不知道该如何解决,我对Java真的很陌生.

I am getting the error Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor at the place marked in the code. I don't know how to fix that, and I'm really new to java.

推荐答案

您必须在使用super()扩展Visitor类的构造函数的第一个类上调用Visitor的构造函数: /p>

You must call the Visitor's constructor on the first like of the constructor that extends the Visitor's class, using super():

public User(String UName,String Ufname){
    super();
    setName(UName);
    setFname(Ufname);
}

,如果您还希望Admin也具有自定义构造函数,则必须调用:

and if you want Admin to have a custom constructor too, you must call:

super(uname, ufname) // String, String - pass the params of the superclass cunstructor

所以:

public Admin() {
    super(someString, someString);
}

这篇关于未定义的构造函数(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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