【问题标题】:Java using default constructorJava使用默认构造函数
【发布时间】:2016-04-10 13:41:14
【问题描述】:

我在我的程序中遇到了构造函数的小问题。它是一个简单的银行数据库,用于存储客户数据。我必须实现在两个账户之间存入、提取和转移现金的方法。我已经实现了那种构造函数来添加新的银行账户:

public Customer() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter id of customer:");
        this.id = scan.nextLine();
        File folder = new File("CustomerDataBase" + File.separator + this.id + ".txt");
        if(folder.exists()) {
            System.out.println("Customer ID already exists!");
            System.exit(1);
        }  
        try {      
            System.out.println("Enter name of customer:");
            this.name = scan.nextLine();
            System.out.println("Enter surname of customer:");
            this.surname = scan.nextLine();
            System.out.println("Enter PESEL number of customer:");
            this.pesel = scan.nextLine();           
            System.out.println("Enter address of customer:");
            System.out.println("    Street:");
            this.adressStreet = scan.nextLine();
            System.out.println("    City:");
            this.adressCity = scan.nextLine();
            System.out.println("    Zip Code: ");
            this.zipCode = scan.nextLine();
            System.out.println("Enter funds of Customer:");
            this.funds = Double.parseDouble(scan.nextLine());
            this.saveCustomer();
            scan.close();
        }catch(NumberFormatException e) {
            System.out.println("Error : " + e);
            System.exit(1);
        }   
    }

然后我有一个方法withdraw

static void withdraw (int amount, int id) {

    File f = new File("CustomerDataBase" + File.separator + String.valueOf(id) + ".txt");
    Scanner fRead;

    Customer tempCustomer = new Customer();

    try{
        fRead = new Scanner(f);
        tempCustomer.id = fRead.nextLine();
        tempCustomer.name = fRead.nextLine();
        tempCustomer.surname = fRead.nextLine();
        tempCustomer.pesel = fRead.nextLine();
        tempCustomer.adressStreet = fRead.nextLine();
        tempCustomer.adressCity = fRead.nextLine();
        tempCustomer.zipCode = fRead.nextLine();
        tempCustomer.funds = Double.parseDouble(fRead.nextLine()) - id;
        fRead.close();
        tempCustomer.saveCustomer();
    }catch(FileNotFoundException e) {
        System.out.println("File not found!");
        System.exit(1);
    }   
}

withdraw 方法从文件中读取数据并且必须将其存储在类中。所以我将对象创建为客户类型。但是我只想使用 Java 提供的“普通”(默认)构造函数,当您不声明自己的构造函数时。 怎么做?我读过super(): 声明,但如果我理解正确,它仅在您从另一个类继承时才有效。

【问题讨论】:

  • 您的customer 类定义了自己的构造函数,因此不会创建默认的普通构造函数...
  • 他声明的构造函数是默认构造函数,因为他将其声明为无参数构造函数
  • 这就是为什么没有“普通”的原因(“默认”这个词在这里有点误导)
  • 顺便说一句 - 您不应该将大部分程序逻辑嵌入到构造函数中。构造函数仅用于初始化。相反,创建另一个方法 - processInput() 并在对象的实例上调用该方法。
  • 只有当 super() 是构造函数的第一行时,才能调用它。如果省略,默认情况下会隐式调用它。所有类都继承自一个类。如果您不显式扩展另一个类,则默认情况下您正在扩展类对象。

标签: java constructor multiple-constructors


【解决方案1】:

每个类都带有一个在类本身中不可见的默认构造函数。 但是,请注意,如果您指定了默认构造函数以外的构造函数,则无法使用默认构造函数,根据下面的@Rustam 评论。 例如,假设您的 Customer 类如下所示:

public class Customer {

    private String name;

    private String lastName;

    private age int;

    private String ssn;

    //default constructor that is NOT visible
    Customer()
    {

    }

    //other constructor given name and lastName
    Customer(name, lastName)
    {
        this.name = name;

        this.lastName = lastName;
    }

    //getters and setters

}

构造函数 Customer() 是默认创建的,您不必将其包含在您的类中。

然后您可以使用默认构造函数创建客户实例,然后您需要使用 setter 来设置属性,如下所示:

public class Test {

    public static void main(String [] args){

        Customer c = new Customer();

        //setting parameters
        c.setName("Jose");

        c.setLastName("Mejia");

    }

}

【讨论】:

  • 没有参数的默认构造函数由编译器隐式添加如果没有为类指定其他构造函数。所以如果类只有一个构造函数 Customer(String name, String lastName),那么你不能用 new Customer() 创建实例。
  • 正确。我忘了将其添加到答案中。我现在补充。你不必 -1 我
猜你喜欢
  • 1970-01-01
  • 2014-03-24
  • 2013-06-08
  • 2015-06-26
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多