Studies from zero Scala (IV) and the introduction of the package, the object

A: bag and introducing

This chapter do not find anything useful, you can use the direct method of java package. Here just for integrity of the article. Bigwigs can directly see the second chapter

package

 Scripture says of too limited. Personal feeling is like you are in front of the line plus package

        package com.test.aa.bb

                object FastLearnScala{
                def main(args: Array[String]): Unit = {
                        println("aa")
                }
        }

Scoping rules

1. A sub package can directly access the contents of the parent package.

2. If someone scala class has been named as its class, you can use the new _root_.scala.collection.mutable.ArrayBuffer [String] absolute path to access the package

(But actually in the course: 1. The default guide you pack all the time very few people use the full path 2. scala been named as a package as the package name)

Tandem package statement

       {com.test.aa.bb Package
              // At this time, com.test.aa inside the package, not visible in this
              Package {dd
              }
       }

File top labeling

       package com.test.aa.bb

              object FastLearnScala{
                     def main(args: Array[String]): Unit = {
                            println("aa")
                     }
       }

  This method is the most common and most used

Package object

       com.test.aa package
       package BB Object {// each package can define an object, some of the parameters of what package
              Val AA = 10
       }
       package {BB
              Object FastLearnScala {
                     DEF main (args: the Array [String]): Unit = {
                            AA // class can be accessed directly
                     }
              }
       }

Visibility package

       com.test Package
       Package aa {
              Object Learn {
                     Private [aa] DEF CC (): = {// Only aa Unit package following classes can access this method
                            the println ( "cc.FastLearnScala")
                     }
              }
       }

       package bb{
              object FastLearnScala{
                     def main(args: Array[String]): Unit = {
                            val learn = com.test.aa.Learn
                            learn.cc()  //方法会报错
                     }
              }
       }

Introduced

       import scala.collection.mutable.Map // import classes
       import scala.collection.mutable._ // if troublesome, the package may be turned by this method, a one-time introduction of all classes

Any place can be introduced by the declaration

       {FastLearnScala Object
              DEF main (args: the Array [String]): Unit = {
                     Import scala.collection.mutable.Map // this import class required may be possible to reduce the maximum class into conflict
                     the println ( "AA")
              }
       }

Renaming and concealment methods

       import scala.collection.mutable. {Map, HashMap} // This method can reduce the level of the front of the same packet writing, but also the real use of more

       import scala.collection.mutable. {Map => scalaMutableMap} // class name may be changed using scalaMutableMap later use in the process

       import scala.collection.mutable. {Map => _} // This Map will hide, not import

Hermit introduced

       import java.lang._
       import scala._
       import Predef._

       // scala three or more default program imported package, scala default override some java classes

       import collection.mutable.{Map=>scalaMutableMap} 

       import scala.collection.mutable. {Map => scalaMutableMap} // two way nature of listed no difference, personally antique, still prefer the above

II: Inheritance

Extended class

       persion class () {
              Final the salary = 0.0 Val
              Final DEF AA () {}
       }
       class the extends the Employee {persion
              Val = 0.0 // the salary will be given, as is the parent class Final
              DEF AA () {} // will be given, because the parent class for the final

       }

  // java substantially uniform and extends keyword, the method is defined as a field or not final can be overridden in a subclass

Overriding methods

       class Persion(){
              val salary =10
              def mm(){
                     println("Persion method aa")
              }
       }
       class Employee extends Persion{
              override val salary =100
              override def mm(){
                     super.mm()
              }
       }

        // override methods, variables, and java is the same as override, if you need to call the parent class's method you can use super

Type checking and conversion

 isInstanceOf方法的使用。

在java中都建议使用多态,在此就不写了

受保护字段和方法

 前面介绍了private[this]方法,这是一个protect[this]方法,具体用法和private[this]一致,只是作用域不同

超类的构造

       class Persion(salary:Int){
              def mm(){
                     println("Persion method aa " +salary)
              }
       }
       class Employee(name:String,salary:Int) extends Persion(salary:Int){

              override def mm(){
                     super.mm()
              }
       }

  //可以在子类的构造方法上构造父类的对象,如果父类有不同的参数名称就不行了

重写字段

       class Persion(){
              def salary = 10
       }
       class Employee() extends Persion(){
              override val salary = 20
       }

       在父类中定义的数据可以在子类中进行覆盖。但是scala只有def,val,var三种类型,具体继承关系如下:

类型 继承后可选类型
def def、val
val val
var var
如果你在顶级父类中定义为var,那你所有的继承类中只能使用var了,非常的局限

匿名子类

       class Persion(){}

       object FastLearnScala{

              def main(args: Array[String]): Unit = {

                     val aa = new Persion(){  //此种定义的方法是定义一个新的类型
                            def greeting = "my name is Fred"
                     }
                     println(aa.greeting)
              }
       }

抽象类

       abstract class dog(){
              def id() = {}
       }

       class Persion extends dog(){
              override def id() = { println(100)}
       }

  //这些内容前面已经介绍过,在此不在此说了

抽象字段

       abstract class dog(){
              val id = 1//var的变量不需要override 因为可以直接使用,废话了
       }

       class Persion extends dog(){
              override val id = 100
       }

构造顺序和提前定义

       class dog(){
              val id = 1
              val array = Array[Int](id)
       }

       class Persion extends dog(){
              override val id = 10
       }

       本来是想将10传递进数组的,但是由于父类的初始化早于子类,导致初始化的数字是1

       class dog(){
              val id = 1
              val array = Array[Int](id)
       }

       class Persion extends{
              override val id = 10
       } with dog

  这种写法个人感觉是Persion是将{}里面的内容作为一个对象继承同事继承了dog,因为scala从左往右依次继承所以最终的结果是10

scala继承层级

第一层级 Any
第二层级 AnyVal AnyRef
第三层级 八种基本类型的unit

scala class(scala Object)

和java class

所有的scala都实现ScalaObject这个接口,但是这个接口没有定义任何方法。继承层级的另一端是NUll和Nothing
NULL类型是所有引用类型的子类型,NULL的实例是null,你可以将null赋给任何引用,但不能赋给值类型,就是不能赋给Int、Long
Nothing类型是所有类型的子类型没有实例,它对于泛型结构时常有用。

对象相等性

AnyRef的eq方式检查的是两个引用是否是指向同一个对象,而AnyRef的equals方法调用的eq方法,当你实现类的时候应该考虑的是重新写equals方法

对于Array方法而言他就没有重新写等于方法,所有可选的比较都是false

       val dog = Array[String]("a")
       val dog1 = Array[String]("a")
       println(dog.eq(dog1))
       println(dog.equals(dog1))
       println(dog == dog1)

Guess you like

Origin www.cnblogs.com/wuxiaolong4/p/11828630.html