C# 继承与多态中的类型转换、对象方法重写
1. 类型转换示例
在 C# 中,类型转换是一个重要的操作。下面是一个类型转换的示例代码:
static void CastingExamples()
{
// A Manager "is-a" System.Object, so we can
// store a Manager reference in an object variable just fine.
object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
// A Manager "is-an" Employee too.
Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1);
GivePromotion(moonUnit);
// A PTSalesPerson "is-a" SalesPerson.
SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90);
GivePromotion(jill);
}
上述代码中, Manager 类的对象可以隐式转换为 System.Object 类型和 Employee
超级会员免费看
订阅专栏 解锁全文
1373

被折叠的 条评论
为什么被折叠?



