6.虚拟和非虚拟函数
下面是一个非虚拟函数
using System; namespace Test2 { class Plane { public double TopSpeed() {return 300.0D; }} class Jet : Plane { public double TopSpeed() { return 900.0D;}} class Airport { static void Main(string[] args) { Plane plane = new Jet(); Console.WriteLine("planes top speed: {0}",plane.TopSpeed()); //300 Console.ReadLine(); } } }
因为TopSpeed()是非虚拟的函数,上面的代码将打印出300。为了修正这个问题,我们需要用virtual声明这个方法,并且在子类中用override重写它,如果不设置Jet()为override,结果依然是300,注意观察下面的例子。
class Plane { public virtual double TopSpeed() { return 300.0D;}} class Jet : Plane { public override double TopSpeed() { return 900.0D;}} class Airport { static void Main(string[] args) { Plane plane = new Jet(); Console.WriteLine("planes top speed: {0}",plane.TopSpeed()); //900 Console.ReadLine(); }
7.隐藏继承成员
在子类中,你可以重复使用变量名,但编译器会提醒你,子类的成员是“隐藏”父类的变量。
public class Elephant { public int Weight = 15000; } public class AsianElephant : Elephant { public int Weight = 13000; } void Main() { Console.WriteLine(new AsianElephant().Weight); //13000 }
如果你真想得到一个新的变量,那么告诉编译器将停止抱怨它,通过使用“new”修饰符(不被与“new”运算符混淆)。
public class AsianElephant : Elephant { public new int Weight = 1300; }
8.重载运算符示例。注意,血多运算符必须重载为一对,如〉、〈
public class Plane { public virtual double TopSpeed() { return 300.0D;} public static bool operator>(Plane one, Plane two) { return one.TopSpeed() > two.TopSpeed(); } public static bool operator<(Plane one, Plane two) { return one.TopSpeed() < two.TopSpeed(); } } class Jet : Plane { public override double TopSpeed() { return 900.0D; } public override string ToString() { return "I'm a Jet"; } } class Airport { static void Main(string[] args) { Plane plane = new Jet(); Console.WriteLine("plane's top speed: {0}",plane.TopSpeed()); Jet jet = new Jet(); Console.WriteLine("jet's top speed: {0}",jet.TopSpeed()); Console.WriteLine("Plane > Jet = {0}", plane > jet); Console.ReadLine(); } }
9.重载参数
通常在编译时根据声明的类型参数来决定调用重载方法。即使“mamal”的对象是真正的Tiger型,编译器将调用Mammal重载 - 除非它转换为“dynamic”的类型,在这种情况下,它会调用此基础上该方法真正的对象类型。
using System; namespace ScratchPad { public class Linnaeus { public class Mammal {} public class Tiger : Mammal{} public static void Zoo(Mammal mammal) { Console.Out.WriteLine("mammal"); } public static void Zoo(Tiger tiger) { Console.Out.WriteLine("tiger"); } public static void Main(string[] args) { Mammal mammal = new Tiger(); Zoo(mammal); //writes mammal because variable is that type Zoo((dynamic) mammal); //writes tiger because the object is a Tiger Console.ReadKey(); } } }
10.使用属性访问器方法的例子。注意set和value变量的特殊用途。
public class Plane { protected double mySpeed = 300.0D; public double TopSpeed { get {return mySpeed;} set { mySpeed = value; } } } class Jet : Plane { public Jet() { TopSpeed = 900.0D; } } class Airport { static void Main(string[] args) { Plane plane = new Plane(); Console.WriteLine("plane's top speed: {0}",plane.TopSpeed); Jet jet = new Jet(); Console.WriteLine("jet's top speed: {0}",jet.TopSpeed); Console.ReadLine(); } }
11.输出当前时间
DateTime dt = DateTime.Now; Console.WriteLine("Current Time is {0} ",dt.ToString());
要指定一个格式: dt.ToString("yyyy/MM/dd")
当前独立的通用格式: dt.ToString("u")
将输出 "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"
12.写入几行文字到文件中
using System.IO; ... try { StreamWriter streamWriter = new StreamWriter("tmp.txt"); streamWriter.WriteLine("This is the first line."); streamWriter.WriteLine("This is the second line."); } finally { if(streamWriter != null) { streamWriter.Close(); } }
未完待续。