Scala-- class definition

1, the class definition method

com.itstar.scala.test Package 

class student1 {
Private stuId var: Int = 0
Private stuname var: String = "Jack"
Private stuAge var: Int = 22 is
Private [the this] stuSex var: String = "MALE"

DEF getStuName () this.stuName =
// first equal sign is a logic function, the second equal sign is the assignment
DEF setStuName (newName: String) = this.stuName = newName

}
// Note that object class name and may not be the same, if the same thing, this object is called class of objects associated
object student1 {
DEF main (args: Array [String]): Unit = {
var = STU1 new new student1
println (stu1.getStuName ())
stu1.setStuName ( "Tom")
println (stu1.getStuName ())
println ( "============================")
// When a property is private time, scala will automatically generate a corresponding set get method, the method name with the name attribute is consistent
println (stu1.stuName + "\ t" + stu1.stuAge + "\ t" + stu1. stuId)
// If you want private property is not being accessed, then it is modified with private [the this]
println (stu1.stuSex)

}
}

 

 

 

Guess you like

Origin www.cnblogs.com/jeff190812/p/11823547.html
Recommended