1.类型转换:
ParentClass a = new ChildClass();
ChildClass c = (ChildClass) a;
ParenClass b = new ParentClass();
//false
b is ChildClass;
2.定义抽象类和抽象方法:
(1).只有在抽象类中才能定义抽象方法;
(2).无法实例化抽象类;
(3).抽象方法必须在子类中被覆盖;
(4).可以在抽象类中定义虚函数(不一定要被子类覆盖的virtual函数)。
public abstract class Shape
{
public virtual double Area()
{
return 0;
}
public virtual double Volume()
{
return 0;
}
public abstract double Func();
public abstract string Name
{
get;
}
}
(1). 接口不包含构造函数,方法不包含实现;
(2). 接口中的所有属性和方法都必须在类实现中被定义;
(3). 接口只能声明为public;
public interface IShape
{
double Area();
double Volume();
string Name { get; }
}
public class Point:IShape
{
public Point ()
{
}
public virtual double Area()
{
return 0;
}
public virtual double Volume()
{
return 0;
}
public virtual string Name
{
get
{
return "Point";
}
}
}
4.委托:
C#不允许将方法引用作为参数,而是通过一个创建委托。
namespace TestDelegate
{
class Program
{
private delegate bool Comparator(int a, int b);
private static void Func(Comparator Comp)
{
MessageBox.Show(Comp(1,2).ToString());
}
private static bool Compare(int a, int b)
{
return a < b;
}
static void Main(string[] args)
{
Func(new Comparator(Compare));
}
}
}
5.重载运算符:
public static ComplexNumber operator + ( ComplexNumber x, ComplexNumber y )
{
return new ComplexNumber( x.Real + y.Real, x.Imaginary + y.Imaginary);
}