gpt4 book ai didi

java - 没有找到适合 ClassUser 的构造函数(无参数)

转载 作者:行者123 更新时间:2023-12-02 10:34:01 24 4
gpt4 key购买 nike

我是 Java 新手,我正在尝试了解继承。

错误是没有为ClassUser找到合适的构造函数(无参数)出现在public ClassAdmin(String data),我还没有找到任何对我有帮助的解决方案。

这是我的ClassUser的片段:

public class ClassUser {

public String id;
public String password;
public String name;
public String address;
public String contact;
public String role;


public ClassUser(String id, String password, String name, String address, String contact, String role){

this.id = id;
this.password = password;
this.name = name;
this.address = address;
this.contact = contact;
this.role = role;
}

public ClassUser(String data){
String[] dataArray = data.split(",");
this.id = dataArray[0];
this.password = dataArray[1];
this.name = dataArray[2];
this.address = dataArray[3];
this.contact = dataArray[4];
this.role = dataArray[5];
}

这是我的ClassAdmin的片段:

public class ClassAdmin extends ClassUser{

public String email;

public ClassAdmin(String id, String password, String name, String contact,
String email)
{
super(id+password+name+contact);
this.email = email;
}
public ClassAdmin(String data){ //problem
String[] dataArray = data.split(",");
this.id = dataArray[0];
this.password = dataArray[1];
this.name = dataArray[2];
this.contact = dataArray[3];
this.email = dataArray[4];
}

最佳答案

您需要调用 super 构造函数:

public class ClassAdmin extends ClassUser {

public String email;

public ClassAdmin(String id, String password, String name, String contact,
String email) {
super(id + password + name + contact);
this.email = email;
}

public ClassAdmin(String data) { //problem
super(data);
String[] dataArray = data.split(",");

this.id = dataArray[0];
this.password = dataArray[1];
this.name = dataArray[2];
this.contact = dataArray[3];
this.email = dataArray[4];
}
}

关于java - 没有找到适合 ClassUser 的构造函数(无参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com