📄 asoperator.cs
字号:
using System;
class clsPhone
{
public clsPhone(string number)
{
Number = number;
}
public virtual void Dial(string NumberToCall)
{
System.Console.WriteLine("Beep, Beep: Tone Dial");
System.Console.WriteLine("Calling {0}", NumberToCall);
}
protected string Number;
};
class clsRotaryPhone : clsPhone
{
public clsRotaryPhone(string number) : base(number)
{
}
public override void Dial(string NumberToCall)
{
System.Console.WriteLine("In Rotary Dial, calling: {0}",
NumberToCall);
}
};
class clsAsOperator
{
static void Main()
{
clsPhone BasePhone = new clsPhone("555-1212");
clsRotaryPhone DerivedPhone = new clsRotaryPhone("555-5555");
// Try downcast
DerivedPhone = BasePhone as clsRotaryPhone;
if (DerivedPhone == null)
System.Console.WriteLine("DerivedPhone is NULL");
else
System.Console.WriteLine("BasePhone is downcast");
// Try downcast
try
{
DerivedPhone = (clsRotaryPhone) BasePhone;
}
catch (Exception e)
{
System.Console.WriteLine("Invalid cast exception caught");
}
DerivedPhone = new clsRotaryPhone("555-5555");
// Try upcast
BasePhone = DerivedPhone as clsPhone;
if (BasePhone == null)
System.Console.WriteLine("BasePhone is NULL");
else
System.Console.WriteLine("DerivedPhone has been upcast");
try
{
BasePhone = (clsPhone) DerivedPhone;
System.Console.WriteLine("DerivedPhone again upcast");
}
catch (Exception e)
{
System.Console.WriteLine("Invalid cast exception caught");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -