当前位置:网站首页>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】
List of articles
- Scala stay IDEA Written in HelloWorld
- open IDEA newly build Maven project
- stay IDEA Install in Scala plug-in unit
- stay src New under the directory Scala Catalog
- Right click the item to add Scala Support
- establish HelloWord
- Associated object extension description
- stay Scala Create a new one in Student class
- Decompile
- Scala Source code analysis
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 !
边栏推荐
- IELTS examination process, what to pay attention to and how to review?
- Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
- Relationship between classes and objects
- Cross domain request
- It's too convenient. You can complete the code release and approval by nailing it!
- JS how to realize array to tree
- Deux nombres se remplacent
- Illustrated network: what is gateway load balancing protocol GLBP?
- Application of fire fighting system based on 3D GIS platform
- 企业应用业务场景,功能添加和修改C#源码
猜你喜欢

【路径规划】RRT增加动力模型进行轨迹规划

巩固表达式C# 案例简单变量运算
![[paper reading] cavemix: a simple data augmentation method for brain vision segmentation](/img/41/eb790e7419a158e985fa503bd7dc17.png)
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation

【C】 (written examination questions) pointer and array, pointer

skimage: imread & imsave & imshow
![[paper reading] Tun det: a novel network for meridian ultra sound nodule detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection

Operator explanation

npm install报错 强制安装

他做国外LEAD,用了一年时间,把所有房贷都还清了
![[IELTS reading] Wang Xiwei reading P3 (heading)](/img/19/40564f2afc18fe3e34f218b7b44681.png)
[IELTS reading] Wang Xiwei reading P3 (heading)
随机推荐
2022.07.03 (lc_6111_counts the number of ways to place houses)
Design of emergency lighting evacuation indication system for urban rail transit station
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
Nine Qi single chip microcomputer ny8b062d single key control four LED States
npm install报错 强制安装
GDB common commands
C语言中sizeof操作符的坑
挖财学院开户安全的吗?开户怎么开?
打新债开户注册安全吗?有没有风险的?靠谱吗?
【雅思阅读】王希伟阅读P3(Heading)
Tester's algorithm interview question - find mode
Paper notes multi UAV collaborative monolithic slam
Learn C language from scratch day 024
实战模拟│JWT 登录认证
Illustrated network: what is gateway load balancing protocol GLBP?
Hologres query management and timeout processing
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
MySQL uses the explain tool to view the execution plan
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC