SCAL (c) - the object class

Scala QuickStart (three) - Classes and Objects

one type

The template looks like here, and much like Java

object a {
  def main(args: Array[String]): Unit = {
    val person=new Person
    println(person.name+" "+person.age)
  }
}
class Person{
  var name:String = "steve yu"
  var age = 10
}

Met members of the property uncertain, it is best not to assign null, can be assigned an underscore, need to indicate the type. Int representatives underlined type 0, Double behalf of 0.0, boolean representatives false, objects that represent null, so solve all the problems.

After the improvements:

class Person{
  var name:String = _
  var age:Int = _
}

Second, create objects and methods

1. Creating an object

我们可以加括号,也可以不加
val p=new Person
val p=new Person()
这两种都可以,用var还是val,官方推荐使用val

2. Methods

And create exactly the same function

3. Constructor

  def this(name:String,age:Int){
    this
    this.name=name
    this.age=age
  }

4. The main builder, the builder is not configured on the master 3, the latter is the primary constructor class Person, directly brackets, with parameters, a constructor can write

class Person(){
  var name:String = _
  var age:Int = _
  def this(name:String,age:Int){
    this
    this.name=name
    this.age=age
  }
}

The private constructor

  • Privatization of the main constructor
class Person private(){}
  • Other constructors of privatization
  private def this(name:String,age:Int){
    this
    this.name=name
    this.age=age
  }
  • Advanced use constructor (inname use val modified, then inname is a read-only variable)
class Person (val inname:String){
    var name=inname
}

Third, the package

1. same name class subcontracting process

object Test {
  def main(args: Array[String]): Unit = {
    val tiger1=new com.littlepage.scalapackage.steve.Tiger
    val tiger2=new com.littlepage.scalapackage.yu.Tiger
  }
}

2.scala package can arbitrarily change package, and, after the package changes, the compiler can delete content before and recompile

3. The package of references, references in this first class, if you use the same name of the class, you need to write the full name

example:

val a=new com.littlepage.Tiger

4. A packet object

Why package object. Does not define a class in a package, direct writing method, the error will, therefore, we need a package object technology

package object people{
    //定义一个包对象
}

5. Package object calling a function

object Tiger {
  def main(args: Array[String]): Unit = {
    A.print_a
  }
}

package object A{
  def print_a: Unit ={
    println("a")
  }
}

If your companion class in this package A's inside, you can make calls directly

package A{
  object Tiger {
    def main(args: Array[String]): Unit = {
      print_a
    }
  }
}

We decompiled code and found this package is actually a final of the class, the middle of a static MODULE $ objects

包对象注意点:
1.每个包只有一个包对象
2.包对象的名称需要和包名保持一致

Fourth, access modifier

1 is the default attribute, see the bottom is private, public use of the effect is

2. The method is the default, the default is public

3.private private rights, available only within the class and associated objects

4.protected is protected, the protection of rights can only be accessed by subclasses and can not access the same package

In 5.scala no public key can not be modified with the public properties and methods

注意点:
scala中,我们import一个包中所有的内容应该使用下划线,而不是使用*

Homework

1. Write a class Time, minutes and added to the read-only attribute hours, checking whether a certain moment of time earlier than the other method, before (other: Time): Boolean. Time object should new Time (hrs, min) of the embodiment constructed

class Time(val hrs:Int,val min:Int) {
  val hours:Int=hrs
  val minutes:Int=min

  def before(other:Time):Boolean={
    if(hours!=other.hours)  hours<other.hours
    else minutes<other.minutes
  }
}

2. Write a program all the elements in Java HashMap copied to Scala's HashMap, with the introduction of the statement to rename these two categories.

object Test {
  import java.util.{HashMap=>JavaHashMap}
  import collection.mutable.{HashMap=>ScalaHashMap}
  def main(args: Array[String]): Unit = {
    val jm=new JavaHashMap[Int,String]
    jm.put(1,"小舞")
    jm.put(2,"团团")
    jm.put(2,"怪兽")
    jm.put(4,"S.X")
    val sm=new ScalaHashMap[Int,String]()
    for(k<-jm.keySet().toArray()){
      sm.put(k.asInstanceOf[Int],jm.get(k))
    }
    println(sm.mkString(" "))
  }
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11590240.html