如何在构造函数中添加一些操作?
我相信这个问题很天真,但我发现的只是从构造函数中调用其他构造函数。我需要调用一个方法。我的类(开始):
class ScopedIterator[T](val iter : Iterator[T])
{
private var had_next : Boolean;
private var value : T;
moveNext();
...
所以我想要一个带有单个参数的构造函数,并在这样的构造函数中调用方法 moveNext。就这样。
当我编译代码时出现错误:
错误:抽象成员可能没有私有修饰符
私有变量 had_next :布尔值;
对于价值也是如此。
我将其更改为:
class ScopedIterator[T]
{
private var had_next : Boolean;
private var value : T;
private var iter : Iterator[T];
def this(it : Iterator[T]) =
{
iter = it;
moveNext();
}
...
但现在我在“iter = it”上遇到错误:
错误:应为“this”,但找到了标识符。
iter = it;
如何在Scala中编写这样的构造函数?
Naive question I believe, but all I find is just calling other constructors from constructors. I need to call a method. My class (beginning):
class ScopedIterator[T](val iter : Iterator[T])
{
private var had_next : Boolean;
private var value : T;
moveNext();
...
so I would like to have a constructor with single argument, and in such constructor call a method moveNext. That's all.
When I compile the code I get error:
error: abstract member may not have private modifier
private var had_next : Boolean;
and the same for value.
I changed it to:
class ScopedIterator[T]
{
private var had_next : Boolean;
private var value : T;
private var iter : Iterator[T];
def this(it : Iterator[T]) =
{
iter = it;
moveNext();
}
...
But now I get error on "iter=it":
error: 'this' expected but identifier found.
iter = it;
How to write such constructor in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题是您对
had_next
和value
的定义是抽象的:这些成员没有右侧。尝试一下:
这里,
_
表示“默认未初始化值”。例如,在控制台中,以下内容对我有用:第二个问题(“'this'预期...”)出现是因为在 Scala 中,辅助构造函数必须始终调用另一个构造函数作为其第一个语句。例如,您的构造函数可以以
this()
开头。有关更多详细信息,请参阅Scala 编程中的第 6.7 节。The first problem is that your definitions of
had_next
andvalue
are abstract: these members have no right-hand side.Try instead:
Here,
_
means "default uninitialized value". For example, the following works for me in the console:The second problem ("'this' expected...") comes because in Scala, auxiliary constructors must always call another constructor as their first statement. So your constructor could start with
this()
, for instance. For more details, see Section 6.7 in Programming in Scala.默认构造函数是您在声明类时定义的构造函数
例如:
您还可以定义默认构造函数以不带任何参数,就像在代码中一样
然后您可以添加额外的构造函数。添加到类中的所有构造函数都需要调用另一个构造函数作为构造函数的第一条语句。如果省略,您会得到“预期但找到标识符”。
让我们看一个例子:
在上面的代码中
new classconstructor() 不执行任何操作,因为空构造函数没有主体。
和
new classconstructor(200) 执行空构造函数 + new 构造函数,您可以在其中添加额外的代码,例如调用 moveNext() 方法。在本例中,它会将 200 打印到控制台。
The default constructor is the one you define when you declare your class
Ex:
You can also define the default constructor to take no parameters like in your code
Then you can add extra constructors. All constructors you add to the class need to call another constructor as the first statement of the constructor. If you omit that you get the "this expected but identifier found".
Let's see an example:
In the code above
new classconstructor() does nothing because the empty constructor doesn't have body.
and
new classconstructor(200) executes the empty constructor + the new one where you can add extra code such as a call to moveNext() method. In this case this one prints 200 to the console.