当前位置:网站首页>4. Scala writes HelloWorld in idea, in-depth analysis of accompanying objects, and association of source packages

4. Scala writes HelloWorld in idea, in-depth analysis of accompanying objects, and association of source packages

2022-07-05 00:30:00 liangzai2048

Scala stay IDEA Written in HelloWorld

open IDEA newly build Maven project

stay IDEA Install in Scala plug-in unit

Settings->Plugins->Scala

restart IDEA Effective

stay src New under the directory Scala Catalog

Right click the item to add Scala Support

establish HelloWord

Object: keyword , Declare a singleton object ( Companion )

 Format :
main  Method : From the outside, you can directly call the executed method                
def  Method name ( Parameter name :  Parameter type ):  return type  = {  Method body  }
 stay Scala You can omit the semicolon after [ ; ]
 First, create a package , And then right-click Scala Class-> choice object Type creation 

Knock one main Direct automatic completion

package day01

//object: keyword , Declare a singleton object ( Companion )
object HelloWorld {
    
  def main(args: Array[String]): Unit = {
    
    println("Hello World")
  }
}
 Running results :
Hello World

The green triangle in front here indicates main Method is executable

Here we can still write Java Code

package day01

//object: keyword , Declare a singleton object ( Companion )
object HelloWorld {
    
  def main(args: Array[String]): Unit = {
    
    println("Hello World")
    System.out.println("Hello Scala")
  }
}
 Running results :
Hello World
Hello Scala

Associated object extension description

stay Java Create a new one in Student class

public class Student {
    
    private String name;
    private Integer age;
    private static  String from = "zhongguo";

    public Student(String name, Integer age) {
    
        this.name = name;
        this.age = age;
    }

    public  void  show(){
    
        System.out.println(this.name + " " + this.age + " " + Student.from);
    }

    public static void main(String[] args) {
    
        Student lz = new Student(" handsome young man ", 24);
        Student diaomao = new Student("diaomao", 23);
        lz.show();
        diaomao.show();
    }
}
 Output results :
 handsome young man  24 zhongguo
diaomao 23 zhongguo

stay Scala Create a new one in Student class

  • Introducing companion objects , With the same name class Accompany each other , Remove java Medium static,from Attribute from student Object call
  • class Student Is equivalent to a constructor ,def Customize printInfo Refer to the above structure for function structure
  • main Function defined in object Student Inside
package day01

class Student(name: String, age: Int) {
    
  def printerInfo(): Unit = {
    
    println(name + " " + age + " " + Student.from)
  }
}
// Introducing companion objects 
object Student{
    
  val from:String = "zhongguo"

  def main(args: Array[String]): Unit = {
    
    val liangzai = new Student("liangzai", 24)
    val daiomao = new Student("daiomao", 25)
    liangzai.printerInfo()
    daiomao.printerInfo()
  }
}
 Running results :
liangzai 24 zhongguo
daiomao 25 zhongguo

Decompile

Student.class

package day01;

import scala.Predef$;
import scala.reflect.ScalaSignature;

@ScalaSignature(bytes = "\006\001}2Aa\003\007\001\037!Aa\003\001B\001B\003%q\003\003\005#\001\t\005\t\025!\003$\021\0251\003\001\"\001(\021\025a\003\001\"\001.\017\025\tD\002#\0013\r\025YA\002#\0014\021\0251c\001\"\0015\021\035)dA1A\005\002YBaa\016\004!\002\0239\002\"\002\035\007\t\003I$aB*uk\022,g\016\036\006\002\033\005)A-Y=1c\r\0011C\001\001\021!\t\tB#D\001\023\025\005\031\022!B:dC2\f\027BA\013\023\005\031\te.\037*fM\006!a.Y7f!\tArD\004\002\032;A\021!DE\007\0027)\021ADD\001\007yI|w\016\036 \n\005y\021\022A\002)sK\022,g-\003\002!C\t11\013\036:j]\036T!A\b\n\002\007\005<W\r\005\002\022I%\021QE\005\002\004\023:$\030A\002\037j]&$h\bF\002)U-\002\"!\013\001\016\0031AQAF\002A\002]AQAI\002A\002\r\n1\002\035:j]R,'/\0238g_R\ta\006\005\002\022_%\021\001G\005\002\005+:LG/A\004TiV$WM\034;\021\005%21C\001\004\021)\005\021\024\001\0024s_6,\022aF\001\006MJ|W\016I\001\005[\006Lg\016\006\002/u!)1H\003a\001y\005!\021M]4t!\r\tRhF\005\003}I\021Q!\021:sCf\004")
public class Student {
    
  private final String name;
  
  private final int age;
  
  public Student(String name, int age) {
    }
  
  public void printerInfo() {
    
    Predef$.MODULE$.println((new StringBuilder(2)).append(this.name).append(" ").append(this.age).append(" ").append(Student$.MODULE$.from()).toString());
  }
  
  public static String from() {
    
    return Student$.MODULE$.from();
  }
  
  public static void main(String[] paramArrayOfString) {
    
    Student$.MODULE$.main(paramArrayOfString);
  }
}

Student$.class

package day01;

public final class Student$ {
    
  public static Student$ MODULE$;
  
  private final String from;
  
  public String from() {
    
    return this.from;
  }
  
  public void main(String[] args) {
    
    Student liangzai = new Student("liangzai", 24);
    Student daiomao = new Student("daiomao", 25);
    liangzai.printerInfo();
    daiomao.printerInfo();
  }
  
  private Student$() {
    
    MODULE$ = this;
    this.from = "zhongguo";
  }
}

From the decompiled code ,Scala It uses entry classes and companion classes , Accompanying objects are placed Student$ Inside

Scala Source code analysis

package scala
final class Array[T](_length : scala.Int) extends scala.AnyRef with java.io.Serializable with java.lang.Cloneable {
    
  def length : scala.Int = {
     /* compiled code */ }
  def apply(i : scala.Int) : T = {
     /* compiled code */ }
  def update(i : scala.Int, x : T) : scala.Unit = {
     /* compiled code */ }
  override def clone() : scala.Array[T] = {
     /* compiled code */ }
}
object Array extends scala.FallbackArrayBuilding with scala.Serializable {
    
  val emptyBooleanArray : scala.Array[scala.Boolean] = {
     /* compiled code */ }
  val emptyByteArray : scala.Array[scala.Byte] = {
     /* compiled code */ }
  val emptyCharArray : scala.Array[scala.Char] = {
     /* compiled code */ }
  val emptyDoubleArray : scala.Array[scala.Double] = {
     /* compiled code */ }
  val emptyFloatArray : scala.Array[scala.Float] = {
     /* compiled code */ }
  val emptyIntArray : scala.Array[scala.Int] = {
     /* compiled code */ }
  val emptyLongArray : scala.Array[scala.Long] = {
     /* compiled code */ }
  val emptyShortArray : scala.Array[scala.Short] = {
     /* compiled code */ }
  val emptyObjectArray : scala.Array[java.lang.Object] = {
     /* compiled code */ }
  implicit def canBuildFrom[T](implicit tag : scala.reflect.ClassTag[T]) : scala.collection.generic.CanBuildFrom[scala.Array[_], T, scala.Array[T]] = {
     /* compiled code */ }
  def newBuilder[T](implicit t : scala.reflect.ClassTag[T]) : scala.collection.mutable.ArrayBuilder[T] = {
     /* compiled code */ }
  def copy(src : scala.AnyRef, srcPos : scala.Int, dest : scala.AnyRef, destPos : scala.Int, length : scala.Int) : scala.Unit = {
     /* compiled code */ }
  def empty[T](implicit evidence$1 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def apply[T](xs : T*)(implicit evidence$2 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def apply(x : scala.Boolean, xs : scala.Boolean*) : scala.Array[scala.Boolean] = {
     /* compiled code */ }
  def apply(x : scala.Byte, xs : scala.Byte*) : scala.Array[scala.Byte] = {
     /* compiled code */ }
  def apply(x : scala.Short, xs : scala.Short*) : scala.Array[scala.Short] = {
     /* compiled code */ }
  def apply(x : scala.Char, xs : scala.Char*) : scala.Array[scala.Char] = {
     /* compiled code */ }
  def apply(x : scala.Int, xs : scala.Int*) : scala.Array[scala.Int] = {
     /* compiled code */ }
  def apply(x : scala.Long, xs : scala.Long*) : scala.Array[scala.Long] = {
     /* compiled code */ }
  def apply(x : scala.Float, xs : scala.Float*) : scala.Array[scala.Float] = {
     /* compiled code */ }
  def apply(x : scala.Double, xs : scala.Double*) : scala.Array[scala.Double] = {
     /* compiled code */ }
  def apply(x : scala.Unit, xs : scala.Unit*) : scala.Array[scala.Unit] = {
     /* compiled code */ }
  def ofDim[T](n1 : scala.Int)(implicit evidence$3 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def ofDim[T](n1 : scala.Int, n2 : scala.Int)(implicit evidence$4 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[T]] = {
     /* compiled code */ }
  def ofDim[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int)(implicit evidence$5 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[T]]] = {
     /* compiled code */ }
  def ofDim[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int)(implicit evidence$6 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[T]]]] = {
     /* compiled code */ }
  def ofDim[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int, n5 : scala.Int)(implicit evidence$7 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[scala.Array[T]]]]] = {
     /* compiled code */ }
  def concat[T](xss : scala.Array[T]*)(implicit evidence$8 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def fill[T](n : scala.Int)(elem : => T)(implicit evidence$9 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def fill[T](n1 : scala.Int, n2 : scala.Int)(elem : => T)(implicit evidence$10 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[T]] = {
     /* compiled code */ }
  def fill[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int)(elem : => T)(implicit evidence$11 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[T]]] = {
     /* compiled code */ }
  def fill[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int)(elem : => T)(implicit evidence$12 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[T]]]] = {
     /* compiled code */ }
  def fill[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int, n5 : scala.Int)(elem : => T)(implicit evidence$13 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[scala.Array[T]]]]] = {
     /* compiled code */ }
  def tabulate[T](n : scala.Int)(f : scala.Function1[scala.Int, T])(implicit evidence$14 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def tabulate[T](n1 : scala.Int, n2 : scala.Int)(f : scala.Function2[scala.Int, scala.Int, T])(implicit evidence$15 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[T]] = {
     /* compiled code */ }
  def tabulate[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int)(f : scala.Function3[scala.Int, scala.Int, scala.Int, T])(implicit evidence$16 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[T]]] = {
     /* compiled code */ }
  def tabulate[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int)(f : scala.Function4[scala.Int, scala.Int, scala.Int, scala.Int, T])(implicit evidence$17 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[T]]]] = {
     /* compiled code */ }
  def tabulate[T](n1 : scala.Int, n2 : scala.Int, n3 : scala.Int, n4 : scala.Int, n5 : scala.Int)(f : scala.Function5[scala.Int, scala.Int, scala.Int, scala.Int, scala.Int, T])(implicit evidence$18 : scala.reflect.ClassTag[T]) : scala.Array[scala.Array[scala.Array[scala.Array[scala.Array[T]]]]] = {
     /* compiled code */ }
  def range(start : scala.Int, end : scala.Int) : scala.Array[scala.Int] = {
     /* compiled code */ }
  def range(start : scala.Int, end : scala.Int, step : scala.Int) : scala.Array[scala.Int] = {
     /* compiled code */ }
  def iterate[T](start : T, len : scala.Int)(f : scala.Function1[T, T])(implicit evidence$19 : scala.reflect.ClassTag[T]) : scala.Array[T] = {
     /* compiled code */ }
  def unapplySeq[T](x : scala.Array[T]) : scala.Option[scala.IndexedSeq[T]] = {
     /* compiled code */ }
}

What you see above is IDEA Source code , We need to connect Scala The source package

1、 download scala-sources-2.12.11.tar.gz Source package

Go to Scala Download from the official website scala-sources-2.12.11.tar.gz

2、 Associated source code

1、 Put our source code package scala-sources-2.12.11.tar.gz Put it in Scala Of lib Under the folder G:\java\scala\scala-2.12.11\lib, And decompress it into scala-2.12.11 Folder

2、 Click on Attach Sources… choice G:\java\scala\scala-2.12.11\lib\scala-2.12.11, This folder , You can see the source code

Then let's check the source code

Hold down Ctrl + Mouse click Array

 By checking the source code, we know , here final class Defined a Array[T] Generic , take String Pass in as a generic 

Viewing the source code is helpful for us to observe Scala The underlying implementation of , It's convenient for us to Scala In depth learning of programming , More conducive to the future spark Study


After all ! Pay attention to the pretty boy !

原网站

版权声明
本文为[liangzai2048]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141119169825.html