【问题标题】:How do I get parameters from the superclass?如何从超类中获取参数?
【发布时间】:2014-07-10 22:46:21
【问题描述】:

好的,所以我有这个类 Insurance 及其构造函数。

public class Insurance
{
protected String pNum, pDate;
protected int yPrem;

public Insurance(String pNum, String pDate, int yPrem)
{
    this.pNum = pNum;
    this.pDate = pDate;
    this.yPrem = yPrem;
}
}

如何使 Auto 扩展保险类?我需要将超类的所有参数都传递给子类吗?

public class Auto extends Insurance
{
private String vehicleID, make, model;
private int year, accidents, age;

public Auto(String pNum, String pDate, int yPrem, String vehicleID,
        String make, String model, int year, int accidents)
{
    super(pNum, pDate, yPrem);
    this.vehicleID = vehicleID;
    this.make = make;
    this.model = model;
    this.year = year;
    this.accidents = accidents;
    age = 2014-year;
}
}

Auto 的参数列表真的需要包含来自超类的所有参数吗? 为了澄清,还有一个 Property 类扩展了 Insurance 类。

【问题讨论】:

  • 是的,你做得对。
  • 这里只有两件事看起来没有完成:1)硬编码的 2014 年(明年运行程序时会发生什么?),以及 2)存储年龄的数据模型(本质上会变成每年都不正确,需要更新)而不是车辆的制造日期(永远不会改变)。但就您的方法而言,您做的是正确的事情。
  • 正如@rgettman 回答中的 cmets 所述,您的类名也可能更精确一些。我相信您的意思是让他们成为InsurancePolicyAutoInsurancePolicy(并且Auto 指的是汽车的保险单,而不是汽车本身),所以如果这是真的,您可能应该重命名它们以使其更清晰.

标签: java class inheritance parameters super


【解决方案1】:

因为Insurance 的唯一public 构造函数包含3 个参数,所以Auto 构造函数必须调用它,传递3 个参数。

但从技术上讲,这些参数不一定来自Auto 构造函数本身。虽然让它们来自 Auto 构造函数的参数是有意义的,但您可以在技术上传递文字,但这在这里没有多大逻辑意义,因为这会限制什么可以传递给Insurance

public Auto(String vehicleID,
    String make, String model, int year, int accidents)
{
    super("someNumber", "20140710", 500);
    this.vehicleID = vehicleID;
    this.make = make;
    this.model = model;
    this.year = year;
    this.accidents = accidents;
    age = 2014-year;
}

如果AutoInsurance 的子类,那么您已经拥有它的方式是最好的方式,即使Java 技术上并不要求它是这种方式。

但是AutoInsurance 吗?存在设计问题。也许Car 需要拥有 Insurance。 (或者Person一个Auto一个Insurance)。

【讨论】:

  • 我认为Auto 本来应该是AutoInsurance,应该改成这样。
【解决方案2】:

没有。在 Insurance 中添加一个不带任何参数的默认构造函数。 公共保险(){ }

调用此构造函数将初始化对象,Insurance 中的参数默认为 null。

【讨论】:

  • 当然,但是如果所有这些属性都是必需的(对于 OP 的特定域来说可能就是这种情况),那么无论如何您都将不得不为它们公开和调用设置器,并且您将失去保证他们确实已经设置好了。所以是的,你说的是可能的,但对于 OP 的例子来说似乎没有意义。
【解决方案3】:

不,没有必要。这取决于您的班级的设计。您还可以使用构造函数链接来重载超类中的构造函数。

public class Insurance
{

  protected String pNum, pDate;
  protected int yPrem;

  public Insurance()
  {
  this("10", "10-10-10", 10)
  }

  public Insurance(String pNum, String pDate, int yPrem)
  {
  this.pNum = pNum;
  this.pDate = pDate;
  this.yPrem = yPrem;
  }

}

在这种情况下,您的子类应定义如下:

  public class Auto extends Insurance
  {
      private String vehicleID, make, model;
      private int year, accidents, age;

  public Auto(String vehicleID, String make, String model, int year, int accidents)
 {

 super(); //If a constructor does not explicitly invoke a superclass constructor, the
         //compiler automatically inserts a call to the no-argument constructor of the
          // superclass
 this.vehicleID = vehicleID;
 this.make = make;
 this.model = model;
 this.year = year;
 this.accidents = accidents;
 age = 2014-year;
 }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多