使用关键字New将类将失去多态性,如下所示的代码,具有多态性.
但将子类的getIncome()用New隐藏,此方法将不再有多态性.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace TestClas
{
class Program
{
static void Main(string[] args)
{
Administrator admin = new Administrator();
{
class Program
{
static void Main(string[] args)
{
Administrator admin = new Administrator();
User user = new User();
showSloary(admin);
showSloary(user);
Console.ReadLine();
}
Console.ReadLine();
}
static void showSloary(User user)
{
Console .WriteLine (user .getIncome ());
}
}
{
Console .WriteLine (user .getIncome ());
}
}
class User
{
public User():this ("guo")
{
}
{
public User():this ("guo")
{
}
public User(string name)
{
this.name = name;
{
this.income = 10;
}
}
{
this.name = name;
{
this.income = 10;
}
}
public virtual int getIncome()
{
return this.income;
}
{
return this.income;
}
private int income;
public const int count =2;
private string name;
}
public const int count =2;
private string name;
}
class Administrator : User
{
public Administrator():this("pas")
{
}
{
public Administrator():this("pas")
{
}
public Administrator(string password):base("guo")
{
this.password = password;
this.reward = 90;
}
{
this.password = password;
this.reward = 90;
}
public override int getIncome()
{
return base.getIncome() + this.reward;
}
{
return base.getIncome() + this.reward;
}
~Administrator()
{
}
{
}
private int reward;
private string password;
}
}
private string password;
}
}