Member variables
In java, we define a class like this:
public class JavaPerson {
private String name;
private Integer age;
// 省略name、age的setter和getter方法
}
The call is like this:
public static void main(String[] args) {
JavaPerson javaPerson = new JavaPerson();
javaPerson.setAge(18);
javaPerson.setName("张三");
System.out.println("姓名:" + javaPerson.getName() + ",年龄:" + javaPerson.getAge());
}
In scala, it will be a bit more concise. The definition of a class is as follows. Because the definition of a variable in scala must have an initial value, we can define it as an underscore to indicate that it is empty.
class ScalaPerson {
var age: Integer = _
var name: String = _
}
The call is like this:
val scalaPerson = new ScalaPerson
scalaPerson.age = 18
scalaPerson.name = "张三"
println(s"姓名:${scalaPerson.name},年龄:${scalaPerson.age}")
It seems that we are directly calling the properties of the object. In fact, Scala hides the setter and getter. Let’s take a look at the decompiled code. This is the decompiled code of ScalaPerson. You can see that his properties are actually private. There are similar setter and getter methods, such as age() and age_$eq() methods
Sometimes in order to be compatible with the Java framework, setter and getter methods are needed. We can use setter and getter methods @BeanProperty
class ScalaPerson2 {
@BeanProperty
var age: Integer = _
var name: String = _
}
object ScalaPerson2 {
def main(args: Array[String]): Unit = {
val scalaPerson = new ScalaPerson2
scalaPerson.age = 17
scalaPerson.setAge(18)
scalaPerson.name = "张三"
println(s"姓名:${scalaPerson.name},年龄:${scalaPerson.getAge}")
}
}
We know that scala variable declarations include val and var. If val is defined in a class, then this variable is of final type. For example, the name above is defined by val, which is similar to the following java code and only has a get method.
private final String name = "张三";
Constructor
No-parameter construction
If there is no parameter, you can add parentheses to the class name or not
class ScalaConstruct1 {
var name: String = _
var age: Int = _
}
object ScalaConstruct1 {
def main(args: Array[String]): Unit = {
val construct = new ScalaConstruct1
construct.name = "张三"
construct.age = 18
}
}
Parametric constructor
With parameters, the modifiers of the variable include no modifier, val and var. The following examples illustrate these three situations.
class ScalaConstruct2(param1: String, var param2: String, val param3: String) {
}
object ScalaConstruct2 {
def main(args: Array[String]): Unit = {
val construct = new ScalaConstruct2("aa", "bb", "cc")
//println(construct.param1) //error
//construct.param1 = "aaa" // error
construct.param2 = "bbb"
println(construct.param2)
//construct.param3 = "ccc"// error
println(construct.param3)
}
}
Param1 has no modifier, so it is a local variable of ScalaConstruct2, so it cannot be called outside the class.
param2 can be modified, so it can be read and modified.
param3 cannot be modified, so it can only be read but not modified.
Main constructor
The above without parameters and the parameterized constructors placed after the class name are the main constructors, and the main constructor can also execute statements that can be executed in the class.
So after running the following, it will directly print hello ScalaConstruct3
class ScalaConstruct3() {
println("hello ScalaConstruct3")
}
object ScalaConstruct3{
def main(args: Array[String]): Unit = {
new ScalaConstruct3
}
}
Auxiliary constructor
In Java, the class name is used as the name of the constructor. In scala, the auxiliary constructor is declared with this. It must directly or indirectly call the main constructor. The instantiation of a class is similar to java.
class ScalaConstruct4() {
var age: Int = _
var name: String = _
def this(name: String) {
this()
this.name = name
}
def this(name: String, age: Int) {
this(name)
this.age = age
}
}
object ScalaConstruct4 {
def main(args: Array[String]): Unit = {
new ScalaConstruct4
new ScalaConstruct4("张三")
new ScalaConstruct4("张三",18)
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。