質問編集履歴
5
「試したこと」のコードが分かりづらかった為修正。
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
下記のような記述をすれば、一応状況に応じて処理ができそうなのですが、もっと合理的な方法などがあれば知りたいです。
|
9
9
|
|
10
10
|
```Result.java
|
11
|
-
|
11
|
+
class Result<R1, R2, R3> {
|
12
12
|
private R1 r1 = null;
|
13
13
|
private R2 r2 = null;
|
14
14
|
private R3 r3 = null;
|
@@ -44,21 +44,12 @@
|
|
44
44
|
public class Main {
|
45
45
|
public static void main(String[] args) {
|
46
46
|
Result<String, Integer, Boolean> result = new Result<>("hello", null, null);
|
47
|
+
// Result<String, Integer, Boolean> result = new Result<>(null, 2, null);
|
48
|
+
if (result.isExistR1()) {
|
47
|
-
Strin
|
49
|
+
System.out.println(result.getR1());
|
48
|
-
|
50
|
+
} else if (result.isExistR2()) {
|
49
|
-
if (exists) {
|
50
|
-
te
|
51
|
+
System.out.println(result.getR2());
|
51
52
|
}
|
52
|
-
System.out.println(text);
|
53
|
-
|
54
|
-
|
55
|
-
result = new Result<>(null, 1, null);
|
56
|
-
Integer num = 0;
|
57
|
-
exists = result.isExistR2();
|
58
|
-
if (exists) {
|
59
|
-
num = result.getR2();
|
60
|
-
}
|
61
|
-
System.out.println(num);
|
62
53
|
}
|
63
54
|
}
|
64
55
|
```
|
4
「試したこと」のコードが間違っていた為修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -23,10 +23,18 @@
|
|
23
23
|
return r1 != null;
|
24
24
|
}
|
25
25
|
|
26
|
+
public boolean isExistR2() {
|
27
|
+
return r2 != null;
|
28
|
+
}
|
29
|
+
|
26
30
|
// 省略
|
27
31
|
|
28
32
|
public R1 getR1() {
|
29
33
|
return r1;
|
34
|
+
}
|
35
|
+
|
36
|
+
public R2 getR2() {
|
37
|
+
return r2;
|
30
38
|
}
|
31
39
|
|
32
40
|
// 省略
|
@@ -35,15 +43,22 @@
|
|
35
43
|
```Main.java
|
36
44
|
public class Main {
|
37
45
|
public static void main(String[] args) {
|
38
|
-
Result<String, Integer, Boolean> result = new Result<>("hello");
|
46
|
+
Result<String, Integer, Boolean> result = new Result<>("hello", null, null);
|
39
|
-
|
40
47
|
String text = "";
|
41
48
|
boolean exists = result.isExistR1();
|
42
49
|
if (exists) {
|
43
50
|
text = result.getR1();
|
44
51
|
}
|
45
|
-
|
46
52
|
System.out.println(text);
|
53
|
+
|
54
|
+
|
55
|
+
result = new Result<>(null, 1, null);
|
56
|
+
Integer num = 0;
|
57
|
+
exists = result.isExistR2();
|
58
|
+
if (exists) {
|
59
|
+
num = result.getR2();
|
60
|
+
}
|
61
|
+
System.out.println(num);
|
47
62
|
}
|
48
63
|
}
|
49
64
|
```
|
3
試したことのコードに不要なコメントがあった為削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,8 +18,6 @@
|
|
18
18
|
this.r2 = r2;
|
19
19
|
this.r3 = r3;
|
20
20
|
}
|
21
|
-
|
22
|
-
// 省略
|
23
21
|
|
24
22
|
public boolean isExistR1() {
|
25
23
|
return r1 != null;
|
2
「試したこと」のコードが間違えていたため修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,8 +13,10 @@
|
|
13
13
|
private R2 r2 = null;
|
14
14
|
private R3 r3 = null;
|
15
15
|
|
16
|
-
public Result(R1 r1) {
|
16
|
+
public Result(R1 r1, R2 r2, R3 r3) {
|
17
17
|
this.r1 = r1;
|
18
|
+
this.r2 = r2;
|
19
|
+
this.r3 = r3;
|
18
20
|
}
|
19
21
|
|
20
22
|
// 省略
|
1
試したことを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,3 +4,47 @@
|
|
4
4
|
|
5
5
|
のように状況に応じてどちらかの型を返すように指定したいです。
|
6
6
|
|
7
|
+
### 試したこと
|
8
|
+
下記のような記述をすれば、一応状況に応じて処理ができそうなのですが、もっと合理的な方法などがあれば知りたいです。
|
9
|
+
|
10
|
+
```Result.java
|
11
|
+
public class Result<R1, R2, R3> {
|
12
|
+
private R1 r1 = null;
|
13
|
+
private R2 r2 = null;
|
14
|
+
private R3 r3 = null;
|
15
|
+
|
16
|
+
public Result(R1 r1) {
|
17
|
+
this.r1 = r1;
|
18
|
+
}
|
19
|
+
|
20
|
+
// 省略
|
21
|
+
|
22
|
+
public boolean isExistR1() {
|
23
|
+
return r1 != null;
|
24
|
+
}
|
25
|
+
|
26
|
+
// 省略
|
27
|
+
|
28
|
+
public R1 getR1() {
|
29
|
+
return r1;
|
30
|
+
}
|
31
|
+
|
32
|
+
// 省略
|
33
|
+
}
|
34
|
+
```
|
35
|
+
```Main.java
|
36
|
+
public class Main {
|
37
|
+
public static void main(String[] args) {
|
38
|
+
Result<String, Integer, Boolean> result = new Result<>("hello");
|
39
|
+
|
40
|
+
String text = "";
|
41
|
+
boolean exists = result.isExistR1();
|
42
|
+
if (exists) {
|
43
|
+
text = result.getR1();
|
44
|
+
}
|
45
|
+
|
46
|
+
System.out.println(text);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|