5, scala Object-oriented - class

one type

1, define the class

## to define and call
scala> :paste
// Entering paste mode (ctrl-D to finish)

class HelloWord {
  private var name = "Leo"
  def sayHello() {print("Hello, " + name)}
  def getName = name
}

// Exiting paste mode, now interpreting.

defined class HelloWord

scala> val helloWord = new HelloWord
helloWord: HelloWord = HelloWord@eba64a9

scala> helloWord.sayHello()
Hello, Leo
scale> 

scala> print(helloWord.getName)
Leo


2, getter and setter

We often put some type of field is defined as private to complete the package when using Java, so that the outside world can not access.
If time outside access or modify the field, only getter and setter methods provided through the field to achieve.
In Scala is not a getter and setter said.
And value_ = value used respectively in place of the getter and setter.


■ If a field is private, the getter and setter methods is private   

  It is to explain, when we define a field as private time, automatically generate getter and setter can not be used outside.

       That we can not access or modify the field at the point of use outside the field + mode.

  We can access and modify to be done through their own private variables rewrite scala getter and setter, as described above.

■ If the field is val, only getter methods are generated 

  When we need getter and setter can be defined as a variable var

  When we do not need to just getter setter, we can define the variables val

■ If you do not need any getter or setter, the field can be declared as private [this]

We can either set the access method to a private variable time
Either add getter method (in terms of field val) or add getter and setter methods (in terms of field var)
But in Scala, you can not achieve write-only property, that property with a setter but without getter.



##
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
  var name = "leo"
}

// Exiting paste mode, now interpreting.


scala> val s = new Student
s: Student = Student@4cdbe50f

Scale> s.name
res2: String = leo

scala> s.name = "jack"
s.name: String = jack

Scale> s.name
res3: String = jack


3, custom getter setter

// If you just want to have a simple getter and setter methods, then in accordance with the rules of grammar scala provided according to the needs select the appropriate modifier for the field like: var, val, private, private [this]
// but if you want to be able to control their own getter and setter, you can customize getter and setter methods
// custom setter methods must pay attention to grammar limit scala, the signature, = no spaces between parameters


##
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
  private var myName = "leo"
  def name = "your name is " + myName
  def name_=(newName: String) {
    print("you cannot edit your name!")
  }
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s = new Student
s: Student = Student@52f759d7

Scale> s.name
res4: String = your name is leo

scala> s.name = "leo1"
you cannot edit your name!s.name: String = your name is leo


4, only exposed getter method of field

// If you do not want to have a field setter method, it can be defined as val, but at this point you can no longer change the value of the field
// But if you want to be able to expose only a getter method, and can change the value of the field by some method, you need to use private as well as comprehensive custom getter method
// In this case, because the field is private, so the setter and getter are private, there is no exposure to the outside world; modify feld value method they can achieve; they can cover getter method



##
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
  private var myName = "leo"
  
  def updateName(newName: String) {
    if (newName == "leo1") myName = newName
    else println("not accept this new name, " + newName)
  }
  
  def name = myName
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s = new Student
s: Student = Student@4aa8f0b4

scala> s.updateName("leo1")

scala> s.updateName("leo2")
not accept this new name, leo2

Scale> s.name
res2: String = leo1


5, private [this] is used

// If the field decorated with a private, then this field is representative of the class private, in the method of the class, the private field can directly access other objects of the class
// In this case, if you do not want to be field accessible to other objects, you can use private [this], means that the object of private field, you can only access to objects within this



##
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
  private[this] var myAge = 0
  def age_=(newAge: Int) {
  if (newAge > 0) myAge = newAge
  else println("illegal age!!")
  }
  def age = myAge
  def older(s: Student) = {
  myAge > s.myAge
  }
}

// Exiting paste mode, now interpreting.

<console>:22: error: value myAge is not a member of Student
         myAge > s.myAge
                   ^


6, Java-style getter and setter methods

// naming and java getter and setter methods Scala is different, and the field is the way field_ =
// If you want scala getter and seter method to automatically generate java-style, just add to the field notes to @BeanProperty
// At this method generates four, name: String, name _ = (newValue: String): Unit, getName (): String, setName (newValue: String): Unit



###

scala> import scala.reflect.BeanProperty
<console>:10: error: object BeanProperty is not a member of package reflect
        import scala.reflect.BeanProperty
               ^

scala> import scala.beans.BeanProperty
import scala.beans.BeanProperty

scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
   @BeanProperty var name: String = _
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s = new Student
s: Student = Student@59690aa4

scala> s.setName("leo")

scala> s.getName()
res1: String = leo

scala> s.setName("jack")

scala> s.getName()
res3: String = jack


In the main embodiment constructor ### annotated

scala> class Student(@BeanProperty var name: String)
defined class Student

scala> val s = new Student("leo")
s: Student = Student@5f9d02cb

scala> s.getName()
res4: String = leo

scala> s.setName("jen")

scala> s.getName()
res6: String = jen


7, auxiliary constructor

Scala, may define a plurality of auxiliary to the class constructor, the constructor overloads similar to Java, can call each other between the auxiliary constructor, and must call the first main line constructor.


###
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student {
  private var name = ""
  private var age = 0
  def this(name: String) {
    this()
    this.name = name
  }
  def this(name: String, age:Int) {
    this(name)
    this.age = age
  }
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s1 = new Student
s1: Student = Student@78b729e6

scala> val s1 = new Student("leo")
s1: Student = Student@1fe20588

scala> val s1 = new Student("leo",30)
s1: Student = Student@647e447


8、主constructor

// Scala, the name of the main class constructor is put together with different java
Further // class, code is not defined in any of the methods or code blocks, that is, main code constructor, this feeling is not so clear java

###
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student(val name: String, val age: Int) {
  println("your name is " + name + ", your age is " + age)
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s = new Student("leo", 30)
your name is leo, your age is 30
s: Student = Student@6069db50



Main construntor may also by using the default parameter value to the parameter to the default.

###
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student(val name: String = "leo", val age: Int = 30) {
  println("your name is " + name + ", your age is " + age)
}

// Exiting paste mode, now interpreting.

defined class Student

scala> val s = new Student
your name is leo, your age is 30
s: Student = Student@682b2fa



If the primary constructor argument passed what modifications are not, such as name: String, then if the internal class method to use, it will be declared as private [this] name,
Otherwise Without this field, it can only be constructor code uses only.

###
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Student(name: String, age: Int) {
  println("your name is " + name + ", your age is " + age)
}

// Exiting paste mode, now interpreting.

defined class Student
warning: previously defined object Student is not a companion to class Student.
Companions must be defined together; you may wish to use :paste mode for this.

scala> val s = new Student("leo", 30)
your name is leo, your age is 30
s: Student = Student@58a9760d


9, inner classes

Scala, the same class may define an internal class, but the difference is that in Java, the class object inside each external class, are of different classes.

c2.Student class, c1.Student class, different instances of different classes outer class.


###
scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.collection.mutable.ArrayBuffer

class Class {
  class Student(val name: String) 
  val students = new ArrayBuffer[Student]()
  def getStudent(name: String) = {
    new Student(name) 
  } 
}

// Exiting paste mode, now interpreting.

import scala.collection.mutable.ArrayBuffer
defined class Class

scala> val c1 = new Class
c1: Class = Class@50b5ac82

scala> val s1 = c1.getStudent("leo")
s1: c1.Student = Class$Student@45099dd3

scala> c1.students += s1
res0: c1.students.type = ArrayBuffer(Class$Student@45099dd3)

scala> val c2 = new Class
c2: Class = Class@2783717b

scala> val s2 = c2.getStudent("leo")
s2: c2.Student = Class$Student@7e985ce9

scala> c1.students += s2
<console>:16: error: type mismatch;
 found   : c2.Student
 required: c1.Student
       c1.students += s2
                      ^

Guess you like

Origin www.cnblogs.com/weiyiming007/p/11008192.html