Scala 类

class 

1》scala 类通过new A ()

2》辅助构造函数 用 this() 并且必须调用构造函数。并且第一行就要调用。

3》 构造函数写法  和java 不同

class Person(name: String, age: Int) {}҅

创建类同时定义构造函数。

4》主要构造函数的方法体?

 除了成员变量 和方法外全是

object

ဌํstaticֵ҅,objec实现Singleton以及static相似的静态方法
 
相当于java静态方法抽取。

1》有时叫做半生对象。

2》和同名的class 互相访问对方的变量 包含私有变量

3?》继承 App  来省略 main 

下面例子

/**
* @author zhao
*/
class TestClass( x:Int , y:Int) {
val x1=x;
val y1=y;
def this(x:Int)
{

this(x,1);


}
def this()
{

this(1,2);


}



}

object TestClass extends App{
val t1= new TestClass(1,2)
println(t1.x1)
println(t1.y1)
}

原文地址:https://www.cnblogs.com/itxuexiwang/p/6286801.html