using System;
using System.Collections;
public class Nevermore60Customer:Customer
{
public override string GetFunnyString()
{
return "Nevermore60.Nevenmore!";
}
public class Customer
{
public virtual string GetFunnyString()
{
return "Plain ordinary customer.Kaark!";
}
public class SamplesArray
{
public static void Main()
{
Customer Cust1=new Nevermore60Customer();
Nevermore60Customer Cust2;
// Cust1=new Customer();
Console.WriteLine("Customer referencing Customer."+Cust1.GetFunnyString());
Cust1=new Nevermore60Customer();
Console.WriteLine("Customer referencing Nevenmore60Customer:"+Cust1.GetFunnyString());
Cust2=new Nevermore60Customer();
Console.WriteLine("Nevenmore60Customer referencing"+Cust2.GetFunnyString());
}
}
Customer Cust1=new Nevermore60Customer();
如果是重写:则,如果声明的基类对象,指向的是派生类,那么这时调用的是派生类中对基本重写的方法的方法(如上所示)
如果是隐藏基类方法: public New string GetFunnyString()
{
return "Nevermore60.Nevenmore!";
}
则:Customer Cust1=new Nevermore60Customer();此时Cust1调用的仍是声明时候的基类中的GetFunnyString方法.
但二者声明与赋值都是同一个类时,不会发生这种问题.
本文地址:http://com.8s8s.com/it/it42526.htm