-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
498 lines (490 loc) · 13.6 KB
/
Program.cs
File metadata and controls
498 lines (490 loc) · 13.6 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudyCode
{
class Program
{
static void Main(string[] args)
{
//T3D5();
//T3D6();
//T3D7();
//T3D8();
//T3D10();
//T3D12();
//T3D13();
//T3D14();
//PersonThree p3 = new PersonThree();
//p3.Print("张三");
//PersonFive p5 = PersonFive.GetInstance();
//Console.WriteLine($"类实例的Name属性为:{p5.Name}");
//Console.Read();
T4D8();
}
public static void T2D1()
{
string welcomeText = "欢迎你";
Console.WriteLine(welcomeText);
Console.ReadKey();
}
public static void T3D1()
{
bool isOverSpeed = false;
int speed = 80;
isOverSpeed = speed > 100;
if (isOverSpeed)
{
Console.WriteLine("你已超速,为了你的安全请减速");
}
}
/// <summary>
/// 描述string类型的不可变性
/// </summary>
public static void T3D2()
{
string welcomeText = "hello";
welcomeText = "Hi";
Console.WriteLine(welcomeText);
Console.ReadKey();
}
/// <summary>
/// 数组 引用类型
/// TODO:数组下标超出会出现IndexOutOfRangeException异常
/// </summary>
public static void T3D5()
{
int[] array = new int[5];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
public static void T3D6()
{
Complex num1 = new Complex(1, 2);
Complex num2 = new Complex(3, 4);
Complex sum = num1 + num2;
Console.WriteLine($"num1和为{num1}");
Console.WriteLine($"num2和为{num2}");
Console.WriteLine($"sum和{sum}");
Console.ReadKey();
}
/// <summary>
/// if 判断语句
/// </summary>
public static void T3D7()
{
bool condition = true;
int x = 13;
if (condition)
{
Console.WriteLine("条件为真");
}
else
{
Console.WriteLine("条件为假");
}
if (x < 5)
{
Console.WriteLine("x小于5");
}
else if (x < 10)
{
Console.WriteLine("x大于5小于10");
}
else if (x < 20)
{
Console.WriteLine("x大于10小于20");
}
Console.ReadKey();
}
/// <summary>
/// switch 判断语句
/// </summary>
public static void T3D8()
{
int switchVar = 2;
switch (switchVar)
{
case 0:
case 1:
Console.WriteLine("常量为0或1");
break;
case 2:
Console.WriteLine("常量为2");
break;
default:
Console.WriteLine("默认情况");
break;
}
Console.ReadKey();
}
/// <summary>
/// while 循环语句
/// </summary>
public static void T3D9()
{
int i = 0;
while (i < 5)
{
Console.WriteLine($"i为{i}");
i++;
}
Console.ReadKey();
}
public static void T3D10()
{
int i = 5;
Console.WriteLine("while执行结果如下");
while (i<5)
{
Console.WriteLine($"i为{i}");
i++;
}
Console.WriteLine("do-while执行结果如下");
do
{
Console.WriteLine($"i为{i}");
i++;
} while (i<5);
Console.ReadKey();
}
/// <summary>
/// for 循环语句
/// </summary>
public static void T3D11()
{
Console.WriteLine("代码参见T3D5");
Console.ReadKey();
}
/// <summary>
/// foreach 循环语句
/// TODO:必须实现IEnumerabla和IEnumerabla<T>
/// </summary>
public static void T3D12()
{
int[] array = new int[] { 0, 1, 1, 2, 3, 4 };
Console.WriteLine("数组元素如下");
foreach (var element in array)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
/// <summary>
/// 比较continue和break
/// </summary>
public static void T3D13()
{
Console.WriteLine("使用continue退出循环情况");
for (int i = 0; i < 5; i++)
{
if (i==2)
{
continue;
}
Console.WriteLine($"当前值为{i}");
}
Console.WriteLine("使用brerk退出循环情况");
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
break;
}
Console.WriteLine($"当前值为{i}");
}
Console.ReadKey();
}
/// <summary>
/// 比较continue和break
/// </summary>
public static void T3D14()
{
Console.WriteLine("使用brerk退出循环情况");
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
break;
}
Console.WriteLine($"当前值为{i}");
}
Console.WriteLine("循环退出");
Console.WriteLine("使用return退出循环情况");
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
return;
}
Console.WriteLine($"当前值为{i}");
}
Console.WriteLine("循环退出");
Console.ReadKey();
}
/// <summary>
/// T4D8调用方法
/// </summary>
public static void T4D8()
{
PersonEight p8 = new PersonEight();
p8[0] = 1;
p8[1] = 2;
p8[2] = 3;
Console.WriteLine(p8[0]);
Console.WriteLine(p8[1]);
Console.WriteLine(p8[2]);
Console.ReadKey();
}
}
/// <summary>
/// T3D3 枚举 值类型
/// TODO:枚举默认是int类型,我们可以通过“:”指定全新的整数值类型
/// </summary>
enum Gender :byte
{
Female,
ale
}
/// <summary>
/// T3D4 定义结构体 值类型 一般表示轻量级对象
/// </summary>
public struct Point
{
public int x;
public int y;
public Point(int px,int py)
{
x = px;
y = py;
}
}
/// <summary>
/// T3D6 运算符重载
/// </summary>
public struct Complex
{
public int real;
public int imaginary;
public Complex(int real,int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
/// <summary>
/// + 运算符重载 重载关键字operator
/// </summary>
/// <param name="complex1"></param>
/// <param name="complex2"></param>
/// <returns></returns>
public static Complex operator +(Complex complex1,Complex complex2)
{
//TODO:值类型都有无参构造函数
Complex result = new Complex();
result.real = complex1.real + complex2.real;
result.imaginary = complex1.imaginary + complex2.imaginary;
return result;
}
public override string ToString()
{
return $"{real}+{imaginary}i";
}
}
/// <summary>
/// T4D1 声明类
/// </summary>
public class Person
{
//TODO:如果使用readonly修饰字段则不需要在定义时初始化,可以在构造函数中再完成初始化,而使用const修饰则必须在定义时初始化
private readonly string name;
public const int age=18;
protected bool sex;
}
/// <summary>
/// T4D2 属性
/// </summary>
public class PersonTwo
{
private string _name;
private static string _name2;
private int _age;
private bool _sex;
private int _age2;
public string Name
{
get { return _name; }
set {//value是隐式参数
_name = value; }
}
/// <summary>
/// 只读属性不包含set访问器
/// </summary>
public int Age
{
get { return _age; }
}
/// <summary>
/// 只写属性
/// </summary>
public bool Sex
{
private get { return _sex; }
set { _sex = value; }
}
/// <summary>
/// 增加逻辑控制的属性
/// </summary>
public int Age2
{
get { return _age2; }
set
{
if (value<0||value>120)
{
throw new ArgumentOutOfRangeException("AgeIntPropery", value, "年龄必须在0-120之间");
}
_age2 = value;
}
}
/// <summary>
/// 静态属性
/// </summary>
public static string Name2
{
get { return _name2; }
set { _name2 = value; }
}
}
/// <summary>
/// T4D3 方法重载
/// </summary>
public class PersonThree
{
public void Print(string name)
{
Console.WriteLine("输入的值为:"+name);
}
public void Print(int age)
{
Console.WriteLine("输入的值为:" + age);
}
public void Print(string name,int age)
{
Console.WriteLine($"输入的值为:{name},{age}");
}
}
/// <summary>
/// T4D4 构造函数
/// </summary>
class PersonFour
{
//TODO:构造函数可以进行方法重载,构造函数不允许有返回值,构造函数必须和类同名
//TODO:如果没有为类显示定义一个构造函数,C#编译器会自动生成一个函数体为空的无参实例构造函数
private string _name;
public string Name
{
get { return _name; }
}
public PersonFour()
{
_name = "Learning Hard";
}
public PersonFour(string name)
{
this._name = name;
}
}
/// <summary>
/// T4D5 私有构造函数单例模式
/// </summary>
class PersonFive
{
private string _name;
public static PersonFive personFive;
public string Name
{
get { return _name; }
}
private PersonFive()
{
Console.WriteLine("私有构造函数被调用");
this._name = "Learning Hand";
}
public static PersonFive GetInstance()
{
personFive = new PersonFive();
return personFive;
}
}
/// <summary>
/// T4D6 静态构造函数
/// </summary>
class PersonSix
{
//TODO:静态构造函数不能使用任何访问修饰符
//TODO:静态构造函数不能带有任何参数
//TODO:静态构造函数只会执行一次
//TODO:不能直接调用静态构造函数
private static string _name;
/// <summary>
/// 静态构造函数,仅执行一次
/// </summary>
static PersonSix()
{
Console.WriteLine("调用静态构造函数");
_name = "Learning Hard";
}
public static string Name
{
get { return _name; }
}
}
/// <summary>
/// T4D7 析构函数
/// </summary>
class PersonSeven
{
/// <summary>
/// 析构函数用于在类销毁之前释放类实例所使用的托管和非托管资源
/// </summary>
~PersonSeven()
{
Console.WriteLine("调用析构函数");
}
//TODO:析构函数会被隐式的转换成下述代码
//protected override void Finalize()
//{
// try
// {
// Console.WriteLine("析构函数被调用");
// }
// finally
// {
// //调用Object的Finalize方法
// base.Finalize();
// }
//}
//TODO:不能在结构体中定义析构函数,只能对类使用析构函数
//TODO:一个类只能有一个析构函数
//TODO:无法继承或重载析构函数
//TODO:无法显示地调用析构函数,析构函数私有垃圾回收器自动调用的
//TODO:析构函数既没有修饰符也没有参数
}
/// <summary>
/// T4D8 索引器
/// </summary>
class PersonEight
{
private int[] _intarray = new int[10];
public int this[int index]
{
get { return _intarray[index]; }
set { _intarray[index] = value; }
}
}
}