Java时面向对象的编程语言,由于历史原因,Java中还存在着非面向对象的内容:基本类型,null,静态方法等。Scala语言来自于Java,所以天生就是面向对象的语言,而且Scala时纯粹的面相对象的语言,即在Scala中,一切皆为对象。类是抽象的,概念的代表一类事物,比如人类。对象是具体的,实际的,代表一个具体事物。 类是对象的模版,对象是类的一个个体,对应一个实例。
1.类定义
语法
[修饰符] class 类名 {
类体
}
2.创建对象
语法
val | var 对象名 [: 类型] = new 类型[()]
不改变对象的引用(即:内存地址),应该声明为val性质的,否则声明为var,Scala设计者推荐使用val,因为一般来说,在程序中,通常只是改变对象属性的值,而不是改变对象的引用。Scala在声明对象变量时,可以根据创建对象的类型自动推断,所以类型声明可以省略,但当类型和后面new对象类型有继承关系时即多态,就必须写了。
CreateObject .scala
package com.blueicex.learn.classlearn
/**
* @author blueicex
* @date 2020/2/16 12:50
* @objectname CreateObject
* @Mem learn
*/
//Student、Teacher为Person的子类
object CreateObject {
def main(args: Array[String]): Unit = {
var person1 = new Person()
var person2 = new Person
//此处必须显式指出类型Person
var student: Person = new Student()
var teacher: Person = new Teacher
}
}
Student.scala
package com.blueicex.learn.classlearn
/**
* @author blueicex
* @date 2020/2/16 14:18
* @projectname test
* Mem learn
*/
class Student extends Person {
}
Teacher .scala
package com.blueicex.learn.classlearn
/**
* @author Administrator blueicex
* @date 2020/2/16 13:40
* @projectname test
* Mem learn
*/
class Teacher extends Person {
}
3.方法
语法
def 方法名[: 返回值类型] = {
方法体
}
4.构造方法
Scala构造对象需要调用构造方法,并且可以有任意多个构造方法(即Scala中构造器也支持重载),Scala类的构造器包括:主构造器和辅助构造器。
语法
class 类名(形参列表) { //主构造器
def this(形参列表){ //辅助构造器
}
def this(形参列表){ //辅助构造器可以有多个…
}
}
A.主构造器、辅助构造器
package com.blueicex.learn.classlearn
/**
* @author blueicex
* @date 2020/2/16 12:47
* @objectname Peron
* @Mem learn
*/
//主构造器
class Person(inName: String="blueicex", inAge: Int=33) {
var name: String = inName
var age: Int = inAge
//辅助构造器
def this(inName: String) {
this(inName, 40)
}
def out(): Unit = {
println(name + this.name)
println(age + this.age)
}
override def toString: String = {
this.name + this.age
}
}
Scala构造器的作用是完成对新对象的初始化,构造器没有返回值。 主构造器的声明直接放置于类名之后。主构造器会执行类定义中的所有语句,这里可以体会到Scala的函数式编程和面向对象编程融合在一起,即:构造器也是方法(函数),传递参数和使用方法和前面的函数部分内容没有区别。如果主构造器没有参数,小括号可以省略,构建对象时调用的构造方法的小括号也可以省略。
辅助构造器名称为this(这个和Java是不一样的),多个辅助构造器通过不同参数列表进行区分,在底层就是构造器重载。
B.私有构造器
如果想要主构造器编程私有的,可以在()之前加上private,这样用户只能通过辅助构造器来构造对象了,比如: class Person2 private(){},此时不必在私有构造器内加参数了,因为类此时不让私有构造器构造类了。
辅助构造器的声明不能和主构造器的声明(参数)一致,会发生错误(即构造器名重复)。辅助构造器必须第一行显式调用主构造器,否则语法通不过,与java一样。
package com.blueicex.learn.classlearn
/**
* @author blueicex
* @date 2020/2/16 12:56
* @projectname test
* Mem learn
*/
object PersonInfo {
def main(args: Array[String]): Unit = {
var personInfo = new PersonInfo()
println(personInfo)
}
}
//私有构造器
class PersonInfo private() {
var name: String = _
var age: Int = _
def this(inName: String = "blueicex", inAge: Int = 33) {
//辅助构造器必须显式调用主构造器,否则语法不通过
//此时的辅助构造器,充当了主构造器的作用
this()
this.name = inName
this.age = inAge
}
def this(inName: String) {
//此时如果调用 this(inAge = 12),编译器就迷惑了,不知道是调用哪个构造起了,所以通不过
//this(inAge = 12)
//此条语句就通过了,因为编译器知道,调用了默认构造器
this(inName = "gege", inAge = 12)
this.name = inName
}
def this(inAge: Int) {
//此时调用的是调用的主构造器,不是调用的 def this(inName: String)
this("gege")
this.age = inAge;
}
override def toString: String = super.toString
}
5.构造器参数
Scala类的主构造器的形参未用任何修饰符修饰,那么这个参数是局部变量。如果参数使用val关键字声明,那么Scala会将参数作为类的私有只读属性使用。如果参数使用var关键字声明,那么Scala会将参数作为类的成员属性使用,并会提供对应的xxx()[类似getter]/xxx_KaTeX parse error: Expected group after '_' at position 190: …自动生成类似xxx(),xxx_̲eq()方法,没有冲突,二者可以共存。
StructureParam.scala
package com.blueicex.learn.classlearn
import scala.beans.BeanProperty
/**
* @author Administrator blueicex
* @date 2020/2/16 14:39
* @projectname test
* Mem learn
*/
object StructureParam {
var class1 = new Class1
var class2 = new Class2
var class3 = new Class3
var class4 = new Class4
def main(args: Array[String]): Unit = {
//不能访问,语法通不过,编译器把inParam当成局部变量使用
//println("class1 inParam"+":"+class1.inParam)
//能读
println("class2 inParam" + ":" + class2.inParam)
//既能读也能写
println("class3 inParam" + ":" + class3.inParam)
class3.param = "liuzexuan"
println("class3 inParam" + ":" + class3.inParam)
//自动生成set、get方法
println("class4 inParam" + ":" + class4.getParam)
class4.setParam("blueicex")
}
}
//不要将构造器参数和成员变量混淆
class Class1(inParam: String = "gege") {
var param = this.inParam
override def toString: String = {
this.param
}
}
class Class2(val inParam: String = "gege") {
var param = this.inParam
}
class Class3(var inParam: String = "gege") {
var param = this.inParam
}
//使用@BeanProperty自动生成get和set函数
class Class4(val inParam: String = "gege") {
@BeanProperty
var param = this.inParam
}
————Blueicex 2020/2/15 18:06 blueice1980@126.com