먼저 스칼라에 대한 몇 가지 기본 사항을 설명하겠습니다.
스칼라에서는
scala> class Demo(a: String, b: Int) {
| def stringify: String = a + " :: " + b
| }
// defined class Demo
당신은 그 class
의 인스턴스를 생성하는 데 사용되는 스칼라에 주어진 청사진으로 class
생각할 수있는, 다음과 같이 클래스를 정의합니다. 여기에 class Demo
의 모든 인스턴스에는 String
및 b
이 Int
이고 하나의 방법이 stringify
이고 두 번째 속성이 a
이고 String
을 반환합니다. 여기
scala> val demo1 = new Demo("demo1", 1)
// demo1: Demo = [email protected]
scala> demo1.getClass
// res0: Class[_ <: Demo] = class Demo
demo1
는 class
Demo
의 인스턴스이며 type
Demo
있다.
스칼라에는 특별히 생성 된 내부 클래스의 인스턴스 인 object
의 개념이 있습니다.
scala> object OtherDemo {
| val a: Int = 10
| }
// defined object OtherDemo
scala> DemoObject.getClass
// res2: Class[_ <: OtherDemo.type] = class OtherDemo$
여기
OtherDemo
는 특별히
class
OtherDemo$
생성의 인스턴스 만하고
type
OtherDemo.type
을 보유한다.
그리고는 스칼라
scala> case class AnotherDemo(a: Int)
// defined class AnotherDemo
이 만들어집니다에 case class
있다뿐만 아니라 class
AnotherDemo
하지만 우리는 동반자 개체를 호출 또한 object
AnotherDemo
. 어느
class AnotherDemo(a: Int)
object AnotherDemo {
def apply(a: Int): AnotherDemo = new AnotherDemo(a)
def unapply(anotherDemo: AnotherDemo): Option[Int] = Some(anotherDemo.a)
// And many more utility functions
}
우리는
class
AnotherDemo
의
companion object
으로이
object
AnotherDemo
를 호출하는 것과 동일합니다. 클래스 이름은 대문자로 시작해야 스칼라에서 우리는 두 가지 방법으로
AnotherDemo
의 인스턴스를 만들 수 있습니다
, 또한
// By using new keyword, as we can do for any class
scala> val anotherDemo1 = new AnotherDemo(1)
// anotherDemo1: AnotherDemo = AnotherDemo(1)
// Or we can use `apply` method provided by companion object
scala> val anotherDemo2 = AnotherDemo(2)
// anotherDemo2: AnotherDemo = AnotherDemo(2)
scala> anotherDemo1.getClass
// res6: Class[_ <: AnotherDemo] = class AnotherDemo
scala> anotherDemo2.getClass
// res7: Class[_ <: AnotherDemo] = class AnotherDemo
scala> AnotherDemo.getClass
// res8: Class[_ <: AnotherDemo.type] = class AnotherDemo$
. 이렇게하면 작은 글자로 시작해야하는 인스턴스 변수와 쉽게 구별 할 수 있습니다. 이것은 혼란을 피하는 데 도움이됩니다.
이제는 a: String
이 아니고 a: string
이 아닌 것으로 가정합니다.그것은 실제로 동일합니다
scala> var temp = List(Test("lol","lel",1,2))
// temp: List[Test] = List(Test(lol,lel,1,2))
이제
scala> case class Test(
| a: String,
| b: String,
| c: Int,
| d: Int
| )
// defined class Test
, 당신이 쓰는,
var temp = List.apply(Test.apply("lol","lel",1,2))
또는
val test1 = Test.apply("lol","lel",1,2)
var temp = List.apply(test1)
Test.apply
의 Test
하지 요입니다 ur class Test
그러나 companion object Test
. 그리고 Test.apply
를 호출하면 마지막으로 List
Test
의 인스턴스를 포함 List[Test]
type
의를 얻을 수 List.apply
에 전달되는 class Test
의 인스턴스를 반환합니다.
는하지만
scala> var total = List(Test)
// total: List[Test.type] = List(Test)
당신은 List
그 companion object
Test
의를 포함하는 유형 List[Test.type]
의 작성이를 쓸 때.
는
초점 total: List[Test.type]
부분에 ...이 total
그것이 value/instance
List[Test.type]
type
의 가리 할 것을 의미하고, 무엇을 가리 키도록 거부 할 type
List[Test.type]
의 variable
것을 의미한다.
... 지금이 작업을 수행하려고하는,에 해당
total = total ::: temp
, 실제로
val x = total ::: temp
total = x
,
val x = temp.:::(total)
total = x
지금이 val x = total ::: temp
보고 ,
scala> val x = total ::: temp
// x: List[Serializable] = List(Test, Test(lol,lel,1,2))
너는이 x
이 List[Serializable]
인 것을 본다. 당신이 total = x
을 시도 할 때, 당신은 오류 다음 total
가 List[Test.type]
이 필요하지만 당신은 그것을 List[Serializable]
을 제공하는 것을 의미
scala> total = x
// <console>:13: error: type mismatch;
// found : List[Serializable]
// required: List[Test.type]
// total = x
// ^
을 얻을 것이다.