-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter8.cs
More file actions
180 lines (145 loc) · 5.85 KB
/
Chapter8.cs
File metadata and controls
180 lines (145 loc) · 5.85 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
using System;
using System.Reflection;
namespace Chapter8{
class LimitedInt
{
const int MaxValue = 100;
const int MinValue = 0;
public static explicit operator int(LimitedInt li)
{
return li.TheValue;
}
public static implicit operator LimitedInt(int x)
{
LimitedInt li = new LimitedInt();
li.TheValue = x;
return li;
}
private int _theValue = 0;
public int TheValue
{
get{return _theValue;}
set{
if(value < MinValue)
_theValue = 0;
else
_theValue = value > MaxValue
? MaxValue
: value;
}
}
public static LimitedInt operator -(LimitedInt x)
{
//在这个奇怪的类中,取一个值得负数等于0
LimitedInt li = new LimitedInt();
li.TheValue = 0;
return li;
}
public static LimitedInt operator -(LimitedInt x, LimitedInt y)
{
LimitedInt li = new LimitedInt();
li.TheValue = x.TheValue - y.TheValue;
return li;
}
public static LimitedInt operator +(LimitedInt x, double y)
{
LimitedInt li = new LimitedInt();
li.TheValue = x.TheValue + (int)y;
return li;
}
}
class SomeClass
{
public int Field1 = 0;
public int Field2 = 0;
public void Method1(){}
public int Method2(){return 1;}
}
class Program
{
public static void MainTest()
{
Console.WriteLine("{0}", 1024); //整数字面量
Console.WriteLine("{0}", 3.1416); //双精度型字面量
Console.WriteLine("{0}", 3.1416f); //浮点型字面量
Console.WriteLine("{0}", true); //布尔型字面量
Console.WriteLine("{0}", 'x'); //字符型字面量
Console.WriteLine("{0}", "Hi there"); //字符串字面量
Console.WriteLine("******************************************");
string rst1 = "Hi, there!";
string vst1 = @"Hi, there!";
string rst2 = "It started, \"Four socre and seven...\"";
string vst2 = @"It started, ""Four socre and seven...""";
string rst3 = "Value 1 \t 5, Val2 \t 10";
string vst3 = @"Value 1 \t 5, Val2 \t 10";
string rst4 = "C:\\Program Files\\Microsoft\\";
string vst4 = @"C:\Program Files\Microsoft\";
string rst5 = " Print \x000A Multiple \u000A Lines";
string vst5 = @" Print
Multiple
Lines";
Console.WriteLine(rst1);
Console.WriteLine(vst1);
Console.WriteLine(rst2);
Console.WriteLine(vst2);
Console.WriteLine(rst3);
Console.WriteLine(vst3);
Console.WriteLine(rst4);
Console.WriteLine(vst4);
Console.WriteLine(rst5);
Console.WriteLine(vst5);
Console.WriteLine("********************************************");
Console.WriteLine("0.0f % 1.5f is {0}", 0.0f % 1.5f);
Console.WriteLine("0.5f % 1.5f is {0}", 0.5f % 1.5f);
Console.WriteLine("1.0f % 1.5f is {0}", 1.0f % 1.5f);
Console.WriteLine("1.5f % 1.5f is {0}", 1.5f % 1.5f);
Console.WriteLine("2.0f % 1.5f is {0}", 2.0f % 1.5f);
Console.WriteLine("2.5f % 1.5f is {0}", 2.5f % 1.5f);
Console.WriteLine("********************************************");
int x = 5, y = 4;
Console.WriteLine("x == x is {0}", x == x);
Console.WriteLine("x == y is {0}", x == y);
Console.WriteLine("********************************************");
int a = 5, b;
b = a++;
Console.WriteLine("b : {0}, a : {1}", b, a);
a = 5;
b = ++a;
Console.WriteLine("b : {0}, a : {1}", b, a);
a = 5;
b = a--;
Console.WriteLine("b : {0}, a : {1}", b, a);
a = 5;
b = --a;
Console.WriteLine("b : {0}, a : {1}", b, a);
Console.WriteLine("********************************************");
LimitedInt li = 500;
int value = (int)li;
Console.WriteLine("li:{0}, value:{1}", li.TheValue, value);
Console.WriteLine("li:{0}, value:{1}", li, value);
Console.WriteLine("********************************************");
LimitedInt li1 = new LimitedInt();
LimitedInt li2 = new LimitedInt();
LimitedInt li3 = new LimitedInt();
li1.TheValue = 10; li2.TheValue = 26;
Console.WriteLine("li1: {0}, li2: {0}", li1.TheValue, li2.TheValue);
li3 = -li1;
Console.WriteLine("-{0} = {1}", li1.TheValue, li3.TheValue);
li3 = li2 - li1;
Console.WriteLine("{0} - {1} = {2}", li2.TheValue, li1.TheValue, li3.TheValue);
li3 = li1 - li2;
Console.WriteLine("{0} - {1} = {2}", li1.TheValue, li2.TheValue, li3.TheValue);
Console.WriteLine("***********************************************");
Type t = typeof(SomeClass);
FieldInfo[] fi = t.GetFields();
MethodInfo[] mi = t.GetMethods();
foreach(FieldInfo f in fi)
Console.WriteLine("Field:{0}", f.Name);
foreach(MethodInfo m in mi)
Console.WriteLine("Method:{0}", m.Name);
Console.WriteLine("**********************************************");
SomeClass s = new SomeClass();
Console.WriteLine("Type s : {0}", s.GetType().Name);
}
}
}