简体   繁体   中英

Java subclass takes 1 less argument in the constructor

I'm writing a simple program that displays a persons card number, name and limit in Java fx. How can i make it so that a subclass inherits one less argument in its constructor? I have 2 different card types, a debit card, that has no limit, and a credit card that I will give a limit when creating the object.

public AccountData(String id, String name, int limit) {
    this.id = id;
    this.name = name;
    this.limit = limit;
}

So when creating a debit card subclass that extends AccountData, I do not want the limit argument, because it does not have one.

Thank you in advance.

The clean way would be to leave the limit out of your AccountData class (since it is obviously not a common property) and only introduce it in your credit card class:

public class AccountData {
    private String id;
    private String name;

    public AccountData(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class CreditCardAccountData extends AccountData {
    private int limit;

    public CreditCardAccountData(String id, String name, int limit) {
        super(id, name);
        this.limit = limit;
    }
}

If you want to stick to your current approach, you can add a second constructor to your AccountData class and eg set the limit to a default value (maybe Integer.MAX_VALUE or -1 , although the former seems more appropriate):

public AccountData(String id, String name) {
    this(id, name, Integer.MAX_VALUE);
}

I have 2 different card types, a debit card, that has no limit, and a credit card that I will give a limit when creating the object.

Then presumably CreditCard and DebitCard are subclasses of the AccountData type; so make the limit a property of the CreditCard subclass, not AccountData :

class AccountData {
  AccountData(String id, String name) { ... }
}

class CreditCard extends AccountData {
  CreditCard(String id, String name, int limit) {
    super(id, name);
    this.limit = limit;
  }
}

class DebitCard extends AccountData {
  DebitCard(String id, String name) {
    super(id, name);
  }
}

只需在超类中为借记卡添加另一个构造函数,该构造函数需要较少的参数

Just write a new constructor with less argument something like this:

class DebitCard extends AccountData {

 public DebitCard(String id, String name) {
    super(id, name, Integer.MAX_VALUE);
 }

}

Conceptually, I will suggest to rework on design as it won't be a good idea to have one child class with different sets of inherited members and other child class having different, both inheriting the same parent class.But, for adhoc, I can suggest to make private int limit , in AccountData and CreditCard class. In this, way you can protect your member. If you just have a constructor with less argument, will create issues. As, if limit is public, it can be accessed from DebitCard class as it will inherit. So, better to make private int limit to both AccountData and CreditCard class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM