我正在用 Java 编写程序,但遇到了这个问题。
我创建了一个抽象父类(super class) Customer
和一个子类 RegisteredCustomer
,当然还有主类。我找不到在 main 中使用 RegisteredCustomer
构造函数的方法。
消息 RegisteredCustomer 类型的方法 RegisteredCustomer(String, long, String, String) 未定义
即使我在 RegisteredCustomer
中使用这些参数创建了准确的构造函数>。
我已经尝试过 RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em);
和 Customer.RegisteredCustomer.RegisteredCustomer(fn, tel, adr, em);
注册客户
public class RegisteredCustomer extends Customer {
private static int count = 0;
private int id;
private String email;
private String password;
public RegisteredCustomer(String fullName, long telephone, String adress, String email) {
super(fullName, telephone, adress);
this.id = ++ count;
this.email = email;
Customer.getCustomers().add(Customer.getCustomers().size() , this);
}
主要
RegisteredCustomer.RegisteredCustomer(fn, tel, adr, em);
最佳答案
使用 RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em);
您尝试调用 RegisteredCustomer
类的静态方法 RegisteredCustomer
,它不存在,因此它告诉您该方法未定义。
下面的代码是您尝试调用的方法的示例。
public class RegisteredCustomer {
...
public static void RegisteredCustomer(String fullName, long telephone,
String adress, String email) {
...
}
}
创建 RegisteredCustomer
实例的正确方法是调用:
new RegisteredCustomer(fn , tel , adr , em);
关于java - 试图在main中调用子类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56227173/