English 中文(简体)
梯度(初级/辅助/初级)的构造
原标题:constructors in scala (primary/auxiliary/default primary)

<Cay Horstmann s book ,“关于不住院的情景使我感到迷惑。 缩略语

ex 5.10 : Consider the class

class Employee(val name: String, var salary: Double) {
  def this() { this("John Q. Public", 0.0) }
}

承诺使用明确的田地和违约的主要建筑商。

我不相信我会这样做。 你们有些人能否提出解决办法?

然而,如果试图解决这项工作,我也许会知道我以前曾通知过一些关于主要建筑商和<代码>val的田地(如你可以看到的,我不敢肯定):

Am I right if 我请我说,val field (as name/code> in the 只能通过<条码>、主要的构造,而不是通过<条码>辅助。 在后一种情况下,汇编者将视其为向一个<代码>val<>/code>的调任,从而产生错误。

首先,我想到val 领域,作为与 j最后领域相当的粗略田,预计首次在任何建筑商中分配这些田地是合法的,但似乎我是错误的。

I am not quite satisfied with what may only be a wild guess so I would appreciate if someone could give me more accurate information about that point.

最佳回答

From "Programming in Scala, 2nd edition" paragraph 6.7:

In Scala, every auxiliary constructor must invoke another constructor of the same class as its first action. The net effect of this rule is that every constructor invocation in Scala will end up eventually calling the primary constructor of the class. The primary constructor is thus the single point of entry of a class.

因此,<代码>val的初始化所有数据都必须在主构体内。

具有明确领域的人可以这样说:

class Employee(n: String = "John Q. Public", s: Double = 0.0) {
  val name = n
  var salary = s
}

or without default parameter values:

class Employee(n: String, s: Double) {
  def this() = this("John Q. Public", 0.0)
  val name = n
  var salary = s
}
问题回答

实际上,我所铭记的是一版无标记的主要建筑商,如:

class Employee {
  private var _name = "John Q. Public"
  var salary = 0.0
  def this(n: String, s: Double) { this(); _name = n; salary = s; }
  def name = _name      
}

显然,这不如界定主要建筑商的田地,这正是我想要做的。





相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...