-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter7.cs
More file actions
284 lines (240 loc) · 6.89 KB
/
Chapter7.cs
File metadata and controls
284 lines (240 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
namespace Chapter7
{
class SomeClass
{
public string Field1 = "base class field";
public void Method1(string value)
{
Console.WriteLine("Base class -- Method1: {0}", value);
}
}
class OtherClass : SomeClass
{
public string Field2 = "derived class field";
public void Method2(string value)
{
Console.WriteLine("Derived class -- Method: {0}", value);
}
}
class SomeClass1
{
public string Field1 = "SomeClass1 Field1";
public void Method1(string value)
{
Console.WriteLine("SomeClass1.Method1: {0}", value);
}
}
class OtherClass1 : SomeClass1
{
new public string Field1 = "OtherClass1 Field1";
new public void Method1(string value)
{
Console.WriteLine("OtherClass1.Method1: {0}", value);
}
}
class SomeClass2
{
public string Field1 = "Field1 -- In the base class";
}
class OtherClass2 : SomeClass2
{
new public string Field1 = "Field1 -- In the derived class";
public void PrintField1()
{
Console.WriteLine(Field1);
Console.WriteLine(base.Field1);
}
}
class MyBaseClass
{
public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass : MyBaseClass
{
new public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class MyBaseClass1
{
virtual public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass1 : MyBaseClass1
{
override public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class MyBaseClass2
{
virtual public void Print()
{
Console.WriteLine("This is the base class.");
}
}
class MyDerivedClass2 : MyBaseClass2
{
override public void Print()
{
Console.WriteLine("This is the derived class.");
}
}
class SecondDerived2 : MyDerivedClass2
{
/*
override public void Print()
{
Console.WriteLine("This is the second derived class.");
}
*/
new public void Print()
{
Console.WriteLine("This is the second derived class.");
}
}
class MyBaseClass3
{
private int _myInt = 5;
virtual public int MyProperty
{
get{return _myInt;}
}
}
class MyDerivedClass3 : MyBaseClass3
{
private int _myInt = 10;
override public int MyProperty
{
get{return _myInt;}
}
}
abstract class AbClass
{
public void IdentifyBase()
{Console.WriteLine("I am AbClass");}
abstract public void IdentifyDerived();
}
class DerivedClass : AbClass
{
override public void IdentifyDerived()
{Console.WriteLine("I am DerivedClass");}
}
abstract class MyBase
{
public int SideLength = 10;
const int TriangleSideCount = 3;
abstract public void PrintStuff(string s);
abstract public int MyInt{get; set;}
public int PerimeterLength()
{
return TriangleSideCount * SideLength;
}
}
class MyClass : MyBase
{
public override void PrintStuff(string s)
{
Console.WriteLine(s);
}
private int _myInt;
public override int MyInt
{
get{return _myInt;}
set{_myInt = value;}
}
}
static public class MyMath
{
public static float PI = 3.14f;
public static bool IsOdd(int x)
{return x%2 == 1;}
public static int Times2(int x)
{return 2*x;}
}
class MyData
{
private double D1;
private double D2;
private double D3;
public MyData(double d1, double d2, double d3)
{
D1 = d1; D2 = d2; D3 = d3;
}
public double Sum()
{
return D1 + D2 + D3;
}
}
static class ExtendMyData
{
public static double Average(this MyData md)
{
return md.Sum() / 3;
}
}
class Program
{
static void MainTest()
{
OtherClass oc = new OtherClass();
oc.Method1(oc.Field1);
oc.Method1(oc.Field2);
oc.Method2(oc.Field1);
oc.Method2(oc.Field2);
Console.WriteLine("*****************************");
OtherClass1 oc1 = new OtherClass1();
oc1.Method1(oc1.Field1);
Console.WriteLine("******************************");
OtherClass2 oc2 = new OtherClass2();
oc2.PrintField1();
Console.WriteLine("*******************************");
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass)derived;
derived.Print();
mybc.Print();
Console.WriteLine("********************************");
MyDerivedClass1 derived1 = new MyDerivedClass1();
MyBaseClass1 mybc1 = (MyBaseClass1)derived1;
derived1.Print();
mybc1.Print();
Console.WriteLine("*********************************");
SecondDerived2 derived2 = new SecondDerived2();
MyBaseClass2 mybc2 = (MyBaseClass2)derived2;
derived2.Print();
mybc2.Print();
Console.WriteLine("**********************************");
MyDerivedClass3 derived3 = new MyDerivedClass3();
MyBaseClass3 mybc3 = (MyBaseClass3)derived3;
Console.WriteLine(derived3.MyProperty);
Console.WriteLine(mybc3.MyProperty);
Console.WriteLine("***********************************");
DerivedClass b = new DerivedClass();
b.IdentifyBase();
b.IdentifyDerived();
Console.WriteLine("***********************************");
MyClass mc = new MyClass();
mc.PrintStuff("This is a string");
mc.MyInt = 28;
Console.WriteLine(mc.MyInt);
Console.WriteLine("Perimeter Length: {0}", mc.PerimeterLength());
Console.WriteLine("************************************");
int val = 3;
Console.WriteLine("{0} is odd is {1}", val, MyMath.IsOdd(val));
Console.WriteLine("{0} * 2 = {1}", val, MyMath.Times2(val));
Console.WriteLine("*************************************");
MyData md = new MyData(3,4,5);
Console.WriteLine("Sum: {0}", md.Sum());
Console.WriteLine("Average: {0}", ExtendMyData.Average(md));
Console.WriteLine("Average: {0}", md.Average());
}
}
}