简体   繁体   English

Java子类在构造函数中减少了1个参数

[英]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. 我正在编写一个简单的程序,以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. 我有2种不同的卡类型,无限制的借记卡和创建对象时会给以限制的信用卡。

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. 因此,当创建扩展AccountData的借记卡子类时,我不需要limit参数,因为它没有一个。

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: 干净的方法是将limit排除在AccountData类之外(因为这显然不是常见属性),而仅将其引入您的信用卡类中:

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): 如果您要坚持当前的方法,则可以向AccountData类添加第二个构造函数,例如,将limit设置为默认值(也许是Integer.MAX_VALUE-1 ,尽管前者似乎更合适):

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. 我有2种不同的卡类型,无限制的借记卡和创建对象时会给以限制的信用卡。

Then presumably CreditCard and DebitCard are subclasses of the AccountData type; 那么,大概CreditCardDebitCardAccountData类型的子类。 so make the limit a property of the CreditCard subclass, not AccountData : 因此,将limit设置为CreditCard子类的属性,而不是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. 从概念上讲,我建议对设计进行重新设计,因为让一个子类具有不同的继承成员集而另一个子类具有不同的继承同一个父类并不是一个好主意,但是对于自组织而言,我可以建议在AccountDataCreditCard类中设置private int limit 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. 因为,如果limit是公共的,则可以从DebitCard类访问它,因为它将继承。 So, better to make private int limit to both AccountData and CreditCard class. 因此,最好对AccountData和CreditCard类都设置私有int限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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