asoperator.cs
来自「vc的原码例子12 vc的原码例子12」· CS 代码 · 共 82 行
CS
82 行
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 + =
减小字号Ctrl + -
显示快捷键?