简体   繁体   English

没有抽象类的arg构造函数

[英]No arg constructor for abstract class

I have an abstract class for which I need to provide a constructor that takes 0 parameters. 我有一个抽象类,我需要为其提供一个接受0参数的构造函数。 I'm just not sure how to construct it or what would go in it. 我只是不确定如何构造它或将要使用什么。

enum Engine {STEAM_ENGINE, STEAM_TURBINE, GAS_TURBINE, DIESEL, ELECTRIC, WIND, HUMAN_FORCE};

public abstract class Ship
{   
    protected String name;
    protected int year;
    protected Engine engineType;

    public Ship(String n, int y, Engine t)
    {
       this.name = n;
       this.year = y;
       this.engineType = t;
    }

    public Ship ()
    {

    }

This is the code I have so far. 这是我到目前为止的代码。 Any input would be appreciated. 任何输入将不胜感激。 Especially because I don't really understand the concept of 0-argument constructors in abstract classes. 特别是因为我不太了解抽象类中的0参数构造函数的概念。

Thanks for the help! 谢谢您的帮助!

This would be similar to deb_rider's answer. 这将类似于deb_rider的答案。 But I would recommend using this form of code: 但是我建议使用这种形式的代码:

public Ship() {
    this("", 0, null);
}

What it does is that it calls another parameterized constructor to initialize the Ship with the default values by making use of the this operator. 它的作用是调用另一个参数化的构造函数,以使用this运算符将Ship初始化为默认值。


Why this() instead of this.x = x; this.y = y;... 为什么用this()代替this.x = x; this.y = y;... this.x = x; this.y = y;... ? this.x = x; this.y = y;...

Simple: Because it's shorter and makes the code more readable :-) 简单:因为它更短并且使代码更具可读性:-)

From the link above: 从上面的链接:

"...From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation . ..." “ ...在构造函数中,您还可以使用this关键字调用同一类中的另一个构造函数。这样做被称为显式构造函数调用

Default constructors or non parameterized constructors are used to initialize the attributes to some default values while creating new object of that class when no data is available. 当没有可用数据时,使用默认构造函数或未参数化的构造函数将属性初始化为某些默认值,同时创建该类的新对象。 You can define your "0-argument" constructor to initialize the values as, 您可以定义“ 0参数”构造函数以将值初始化为

public Ship(){
    this.name="";
    this.year=0;
    ...
}

you can also define some set methods to have the privilege to change the values at the later time. 您还可以定义一些set方法,以有权在以后更改值。 eg, 例如,

public void setName(String name){
    this.name=name;
}

First of all, we should understand why we use constructor of abstract class. 首先,我们应该了解为什么要使用抽象类的构造函数。 Here few points which may help you to understand why we use constructor in abstract class. 这里有几点可以帮助您理解为什么我们在抽象类中使用构造函数。

  1. Initialize data member of a class either assigning some value or making constructor chaining like @ambigram_maker's answer. 初始化类的数据成员,以分配一些值或进行构造函数链接(如@ambigram_maker的答案)。 Constructor chaining means calling another constructor from a constructor. 构造函数链接意味着从一个构造函数调用另一个构造函数。
  2. We use default constructor(Non-parameter or with 0 parameter) to avoid developer mistake while constructor chaining from child class eg 我们使用默认构造函数(非参数或带有0参数)来避免开发人员在从子类进行构造函数链接时出错,例如

    public class MyShip extends Ship { 公共课程MyShip扩展Ship {
    public MyShip () { super(); 公共MyShip(){super(); } } }}

Here we'll get error if didn't put default constructor in Ship class.But mostly we use default constructor for default value of data members like 如果没有在Ship类中放置默认构造函数,则会出现错误,但是大多数情况下,我们将默认构造函数用于数据成员的默认值,例如

 public Ship()
    {
       this.name = "Default Engine name";
       this.year = 2014;//what you want as a default.I assume current year
       this.engineType = Engine.STEAM_ENGINE;//this is default engine type
    }

I mean default constructor is used to assign default value(change as per your requirement like default value of int is 0 but you may need current year as a default) for data member of a class and you don't need to put all data member inside default constructor.You should put that data members inside default constructor which are required to change like if you want this.year = 0; 我的意思是默认构造函数用于为类的数据成员分配默认值(根据您的要求进行更改,例如int的默认值是0,但您可能需要当前年份作为默认值),而无需放置所有数据成员您应该将数据成员放入默认构造函数中,这些数据成员需要更改,就像您希望this.year = 0;一样。 then you may avoid and this is done by compiler. 那么您可以避免,这是由编译器完成的。

Hopefully you get helped. 希望你能得到帮助。

Thanks 谢谢

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

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