MemberwiseClone , GetType / typeof , Boxing / Unboxing , is , overload operator , IComparable , as
GetType 用在 实例上,typeof 用在类上
Console.WriteLine(typeof(Program) == p.GetType());
Console.WriteLine(typeof(Program) == p.GetType());
partial class Program : IA {
...
Program pppp = new Program();
IA aaaa = pppp; // boxing
Program pppp2 = (Program) aaaa; // unboxing
...
Program pppp = new Program();
IA aaaa = pppp; // boxing
Program pppp2 = (Program) aaaa; // unboxing
boxing 不需要明确的进行类型转换(子类型到父类型),unboxing需要明确的进行类型转换(父类型到子类型)。
is : 如果一个变量是一个类/接口的子类,孙子类 ,is 返回 true.
Console.WriteLine('a' is int);
可以通过编译,只是提出一个警告。
重载 运算符
Console.WriteLine('a' is int);
可以通过编译,只是提出一个警告。
重载 运算符
public static Program operator + (Program pp1,Program pp2){
Program pp = new Program();
pp.score = pp1.score + pp2.score;
return pp;
}
1,必须是 静态的,
2,加上关键字: operator
3,不能返回 void
4,true 和 false 必须同时重载。> 和 < 必须同时重载, >= 和 <= 也必须同时重载, == 和 != 必须同时重载.
Program pp = new Program();
pp.score = pp1.score + pp2.score;
return pp;
}
1,必须是 静态的,
2,加上关键字: operator
3,不能返回 void
4,true 和 false 必须同时重载。> 和 < 必须同时重载, >= 和 <= 也必须同时重载, == 和 != 必须同时重载.
一元运算的例子:
public static Program operator +(Program p1){
Program p = new Program();
p.score = Math.Abs(p1.score);
return p;
}
...
pppp2.score = -40;
Console.WriteLine((+pppp2).score);
可以重载的运算符:
Unary operators: +, -, !, ~, ++, --, true, false
Binary operators: +, -, *, /, %, &, |, ^, <<, >>
Comparison operators: ==, !=, <, >, <=, >=
Binary operators: +, -, *, /, %, &, |, ^, <<, >>
Comparison operators: ==, !=, <, >, <=, >=
重载 false / true
public static bool operator true(Program p1){
if (p1.score > 1) return true;
return false;
}
public static bool operator false(Program p1){
if (p1.score > 1) return false;
else return true;
}
。。。
if (p1.score > 1) return false;
else return true;
}
。。。
if (pppp2) {
Console.WriteLine("True {0}",pppp2.score);
} else {
Console.WriteLine("False {0}",pppp2.score);
}
Console.WriteLine("True {0}",pppp2.score);
} else {
Console.WriteLine("False {0}",pppp2.score);
}
接口:IComparable
partial class Program : IA , IComparable {
。。。。
#region IComparable 成员
。。。。
#region IComparable 成员
public int CompareTo(object obj) {
if (obj is Program) {
return this.score - ((Program)obj).score;
}else
throw new NotImplementedException();
}
if (obj is Program) {
return this.score - ((Program)obj).score;
}else
throw new NotImplementedException();
}
#endregion
。。。
Console.WriteLine(Comparer.Default.Compare(pppp2,pppp));
Console.WriteLine("here {0}",pppp.CompareTo(obj));
。。。
Console.WriteLine(Comparer.Default.Compare(pppp2,pppp));
Console.WriteLine("here {0}",pppp.CompareTo(obj));
重载转换操作符
public static implicit operator ConvClass2(ConvClass1 op1)
public static explicit operator ConvClass1(ConvClass2 op1)
class B : A{
。。。
public static explicit operator C(B b){ //B 转换成 C ,必须显示的转换。
C c = new C();
c.name = b.name;
return c;
}
}
class C {
private string _name;
private string _name;
public string name {
set { _name = value;}
get { return _name; }
}
set { _name = value;}
get { return _name; }
}
public static implicit operator B(C c){
B b = new B();
b.name = c.name;
return b;
}
。。。。。。。。。。。。。
B b = new B();
b.name = c.name;
return b;
}
。。。。。。。。。。。。。
B bbb = new B();
C ccc = (C)bbb;//B 转换成 C ,必须显示的转换。
C ccc = (C)bbb;//B 转换成 C ,必须显示的转换。
ccc = new C();
bbb = ccc;//可以隐式的转换。
bbb = ccc;//可以隐式的转换。
as
<operand> as <type>
This is only possible in certain circumstances:
If <operand> is of type <type>
If <operand> can be implicitly converted to type <type>
If <operand> can be boxed into type <type>
If no conversion from <operand> to <type> is possible, then the result of the expression will be null.
If <operand> can be implicitly converted to type <type>
If <operand> can be boxed into type <type>
If no conversion from <operand> to <type> is possible, then the result of the expression will be null.
| < Prev | Next > |
|---|
Last Updated ( Monday, 23 March 2009 18:25 )



