質問編集履歴
1
修正済みコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -80,6 +80,95 @@
|
|
80
80
|
}
|
81
81
|
```
|
82
82
|
|
83
|
+
### 修正済みコード(追加)
|
84
|
+
```C#
|
85
|
+
public class Hello{
|
86
|
+
public static void Main(){
|
87
|
+
|
88
|
+
Car car = new Car(new MiniEngine());
|
89
|
+
Car car2 = new Car(new SuperEngineAdapter(new SuperEngine(1)));
|
90
|
+
|
91
|
+
car.Run(); // 5
|
92
|
+
car2.Run(); // 30
|
93
|
+
|
94
|
+
var line = System.Console.ReadLine();
|
95
|
+
int newGear = 0;
|
96
|
+
bool result = int.TryParse(line, out newGear);
|
97
|
+
if(result){
|
98
|
+
//New Code
|
99
|
+
car.SetEngineGear(newGear);
|
100
|
+
car2.SetEngineGear(newGear);
|
101
|
+
//
|
102
|
+
System.Console.WriteLine($"newGear:{newGear}");
|
103
|
+
}
|
104
|
+
|
105
|
+
car.Run(); //Warning
|
106
|
+
car2.Run(); // newGearに応じて変化
|
107
|
+
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
public class Car{
|
114
|
+
protected IEngine engine;
|
115
|
+
public Car(IEngine engine){
|
116
|
+
this.engine = engine;
|
117
|
+
}
|
118
|
+
|
119
|
+
//New Code
|
120
|
+
public void SetEngineGear(int newGear){
|
121
|
+
engine.SetGear(newGear);
|
122
|
+
}
|
123
|
+
|
124
|
+
public void Run(){
|
125
|
+
float power = engine.Boost();
|
126
|
+
System.Console.WriteLine("power=" + power.ToString());
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
public interface IEngine{
|
131
|
+
float Boost();
|
132
|
+
void SetGear(int newGear); //New Code
|
133
|
+
}
|
134
|
+
public class SuperEngine{
|
135
|
+
int gear;
|
136
|
+
public SuperEngine(int initalGear){this.gear = initalGear;}
|
137
|
+
|
138
|
+
public float GetPower(){
|
139
|
+
if(gear == 0) {return 10;}
|
140
|
+
else if(gear == 1){return 30;}
|
141
|
+
else return 50;
|
142
|
+
}
|
143
|
+
|
144
|
+
public void SetGear(int gear) {this.gear = gear;} //New Code
|
145
|
+
|
146
|
+
}
|
147
|
+
public class SuperEngineAdapter : IEngine
|
148
|
+
{
|
149
|
+
SuperEngine se;
|
150
|
+
public SuperEngineAdapter(SuperEngine se){
|
151
|
+
this.se = se;
|
152
|
+
}
|
153
|
+
public float Boost(){
|
154
|
+
return se.GetPower();
|
155
|
+
}
|
156
|
+
|
157
|
+
public void SetGear(int gear){ se.SetGear(gear);} //New Code
|
158
|
+
}
|
159
|
+
|
160
|
+
public class MiniEngine : IEngine{
|
161
|
+
public float Boost(){
|
162
|
+
return 5;
|
163
|
+
}
|
164
|
+
|
165
|
+
//New Code
|
166
|
+
public void SetGear(int newGear){
|
167
|
+
#warning MiniEngine has no gear choice. It's always 5.
|
168
|
+
}
|
169
|
+
}
|
170
|
+
```
|
171
|
+
|
83
172
|
### 試したこと・調べたこと
|
84
173
|
- [x] teratailやGoogle等で検索した
|
85
174
|
- [x] ソースコードを自分なりに変更した
|