当前位置:网站首页>7. Scala class

7. Scala class

2022-07-05 17:12:00 Don import someone

One . class

1. Class definition

Calse  Class name ( constructors parameters ) {
 Parameter declarations + function +Scala Operation statement 
}

2. Class uses

 Variable / Constant   Object name =new  Class name 

Two . Class inheritance

1. characteristic

  • Same as java The same can only be , Father -> Single line inheritance of children
  • Subclasses use the methods of the parent class with keywords override

2. Use

Calse  Class name ( constructors parameters ) extends  Parent class 
{
 Parameter declarations + function +Scala Operation statement 
}

3. Example

abstract class Father{
    def fun1=1
    def fun2:Int
    println("Father_fun1:"+fun1)
    println("Father_fun2:"+fun2)
}
class Child extends Father{
    override def fun1=3
    def fun2=1
    println("Child_fun1:"+fun1)
    println("Child_fun2:"+fun2)

}
new Child

Father_fun1:3
Father_fun2:1
Child_fun1:3
Child_fun2:1

3、 ... and . Recessive class

'implicit' Calse  Class name ...
  • Implicit classes allow implicit conversations with the primary constructor of the class
  • An implicit class must be in another class / object / Defined in the feature ( Not on the top floor ).
  • An implicit class can only use one non default parameter in its constructor .
  • Any method in the scope of an implicit class cannot have the same name as the implicit class , Members or objects .

Four .object( Single object )

  • Effect =java Static class

1. characteristic

  • Definition cannot take parameters
  • There can only be one single column object in the whole program

2. effect

  • As the entry point of the program

3. Example

object Demo {
   def one() {
      println(" Look, this is a static class ")
   }
}
Demo.one

defined object Demo

原网站

版权声明
本文为[Don import someone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051628042269.html