回答編集履歴

2

インターフェース利用に変更

2023/12/15 04:54

投稿

ozwk
ozwk

スコア13551

test CHANGED
@@ -1,16 +1,23 @@
1
1
  ```kotlin
2
2
  class Outer{
3
- val inner : Inner
3
+ val inner : IInner
4
4
  constructor(){
5
5
  inner = Inner() // OK
6
6
  }
7
- inner class Inner{}
7
+ interface IInner{}
8
+
9
+ private class Inner : IInner{
10
+ constructor(){}
11
+ }
8
12
  }
9
13
 
10
14
  fun main(args: Array<String>) {
11
15
  val outer = Outer() // OK
12
16
  val inner1 = outer.inner // OK
17
+
18
+ val inner2 = Outer.Inner() // error: cannot access 'Inner': it is private in 'Outer'
19
+ val inner3 = outer.Inner() // error: unresolved reference: Inner
13
- val inner2 = Outer.Inner() // constructor of inner class Inner can be called only with receiver of containing class
20
+ val inner4 = Outer.IInner() // error: interface IInner does not have constructors
14
21
  }
15
22
 
16
23
  ```

1

コード変更

2023/12/15 04:18

投稿

ozwk
ozwk

スコア13551

test CHANGED
@@ -1,17 +1,16 @@
1
1
  ```kotlin
2
2
  class Outer{
3
+ val inner : Inner
3
4
  constructor(){
4
- val inner = Inner() // OK
5
+ inner = Inner() // OK
5
6
  }
6
- inner class Inner{
7
+ inner class Inner{}
7
-
8
- }
9
8
  }
10
9
 
10
+ fun main(args: Array<String>) {
11
+ val outer = Outer() // OK
12
+ val inner1 = outer.inner // OK
13
+ val inner2 = Outer.Inner() // constructor of inner class Inner can be called only with receiver of containing class
14
+ }
11
15
 
12
-
13
- fun main(args: Array<String>) {
14
- val outer = Outer()
15
- val inner = Outer.Inner() // constructor of inner class Inner can be called only with receiver of containing class
16
- }
17
16
  ```