写构造函数的问题

huangapple go评论93阅读模式
英文:

Problems in writing Constructors

问题

(在学习面向对象编程概念后第一次练习。回答会相对简单)

(我只是按照测试你技能作业中的指示操作;这就是我为什么不讨论其他类和文件的逻辑)

我需要在一个单独的类中添加3个构造函数,该类本身是从父类继承而来的。

第一个构造函数使用与父类构造函数相同的参数。

第二和第三个构造函数逐个添加参数。

我对第二和第三个构造函数的主体语法感到困惑。

public class House 
   extends Building {
   
// TODO - 放置您的代码在这里。
private String mOwner;
private boolean mPool;

//这个构造函数在Building类中存在。所以,我可以在这里用super关键字使用它,对吗?
public House(int length, int width, int lotLength, int lotWidth){
    super(length, width, lotLength, lotWidth);
}

//在这里使用"this"关键字可以吗?我只是在使用这个文件中已存在的构造函数。
public House(int length, int width, int lotLength, int lotWidth, String Owner){
    this(length, width, lotLength, lotWidth);
    mOwner = Owner;
}

// 这样写对吗?
public House(int length, int width, int lotLength, int lotWidth, String Owner, boolean pool){
    this(length, width, lotLength, lotWidth, Owner);
    mPool = pool;
}

}
英文:

(first time practice after studying OOP concepts. Go little easy in the answers)

(I am just following instructions from a test-your-skills homework; that's why I am not discussing logics of other classes and files)

I have to add 3 constructors in a single class which is itself extended from a parent class.

First constructor uses the same parameters of the constructor from parent class.

Second and third constructor keep adding parameters respectively.

I am confused about syntax of the body of 2nd and 3rd constructors.

public class House 
   extends Building {
   
// TODO - Put your code here.
private String mOwner;
private boolean mPool;

//This constructor exists in Building class. So, I can use it here with super keyword. Right?
public House(int length, int width, int lotLength, int lotWidth){
    super(length, width, lotLength, lotWidth);
}

//Is using "this" keyword okay here? I am just using the constructor existing in this file.
public House(int length, int width, int lotLength, int lotWidth, String Owner){
    this(length, width, lotLength, lotWidth);
    mOwner = Owner;
}

// Is this right?
public House(int length, int width, int lotLength, int lotWidth, String Owner, boolean pool){
    this(length, width, lotLength, lotWidth, Owner);
    mPool = pool;
}

}

答案1

得分: 1

这个概念被称为构造函数链式调用。

就好像这个想法是,如果我们想要初始化一座建筑,但还不确定是否想要它有游泳池呢?如果这个类的构造函数要求我们输入一个布尔类型的值来表示是否有游泳池,那么我们可能会遇到问题。

因此,构造函数链式调用非常有用,因为我们仍然可以初始化一座建筑,而不必事先知道所有的信息。

如果我们不确定是否需要游泳池,我们可以使用以下构造函数:

public House(int length, int width, int lotLength, int lotWidth, String Owner)

你写的一切都没问题。this() 调用也没问题。
如果你还不清楚的话 --
https://beginnersbook.com/2013/12/java-constructor-chaining-with-example/

英文:

This concept is called constructor chaining

Like the idea is what if we wanted to initialize a building and we didn't know if we wanted it to have a pool yet? If our constructor for this class requires us to enter us a value for boolean pool, we could run into problems.

So constructor chaining is useful because we would still be able to initialize a building without knowing all of the information yet.

we would just use this constructor if we didn't know if we wanted a pool:

public House(int length, int width, int lotLength, int lotWidth, String Owner)

Everything you wrote is fine. The this() calls are fine.
If you're still unclear --
https://beginnersbook.com/2013/12/java-constructor-chaining-with-example/

huangapple
  • 本文由 发表于 2020年4月4日 16:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61025321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定