【问题标题】:Trying to call the constructor of subclass in main试图在main中调用子类的构造函数
【发布时间】:2019-10-07 04:36:30
【问题描述】:

我正在用 Java 编写一个程序,我遇到了这个问题。

我创建了一个抽象超类Customer 和一个子类RegisteredCustomer,当然还有主类。我找不到在 main 中使用 RegisteredCustomer 的构造函数的方法。

消息The method RegisteredCustomer(String, long, String, String) is undefined for the type RegisteredCustomer 即使我在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 rc = new RegisteredCustomer(fn, tel, adr, em); 顺便说一句,一般不要将电话号码存储得太长。
  • 你建议我如何存储电话号码?
  • 电话号码是一个字符串。你打算如何将“0118 960 194”存储为long?参见,例如,What's the right way to represent phone numbers?

标签: java windows eclipse


【解决方案1】:

我不确定,但尝试创建一个演示类并在那里编写:

RegisteredCustomer rc = new RegisteredCustomer(fn, tel, adr, em);

然后你可以在那里改变你的对象。

【讨论】:

  • 谢谢,我试过 new RegisteredCustomer(fn , tel , adr , em);
【解决方案2】:

您在 java 中初始化对象的方式似乎存在语法错误。下面是一种写法,

public class TestClass {
    public static void main(String[] args) {
        RgisteredCustomer rc = new RgisteredCustomer("John D", 9175556671L, "NYC", "john.d@gmail.com"); //This is how the base and super class constructors are called
        System.out.println(rc);
    }
}

class Customer {
    private String fullName;
    long telephone;
    String address;
    Customer(String fullName, long telephone, String address) {
        this.fullName = fullName;
        this.telephone = telephone;
        this.address = address;
    }
    public String toString() {
        return fullName + " " + telephone + " " + address;
    }
}

class RgisteredCustomer extends Customer {
    private static int count = 0;
    private int id;
    private String email;
    private String  password;

    public RgisteredCustomer(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);
    }
    public String toString() {
        return super.toString() + " " + email + " " + password;
    }
}

在 Java 中,这个 RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em)/Customer.RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em) 表示调用静态方法。

【讨论】:

    【解决方案3】:

    使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2018-05-25
      • 2018-02-28
      • 1970-01-01
      • 2016-02-29
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多